DS18B20 ONE-WIRE BBC micro: bit

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
DRAgon734235
Posts: 2
Joined: Thu Dec 07, 2017 8:10 am

DS18B20 ONE-WIRE BBC micro: bit

Post by DRAgon734235 » Thu Dec 07, 2017 8:49 am

I make a python code use Mu 0.9.13 at my BBC micro:bit
-------------------------------------------------------------------
from microbit import *

def DS18B20_Delayus(us):
while us>0 :
us=us-1

def ds18b20_init():
pin8.write_digital(1)
DS18B20_Delayus(1)
pin8.write_digital(0)
DS18B20_Delayus(10)
pin8.write_digital(1)

if pin8.read_digital()==0:
return 1
print("init OK")
else:
return 0
print("init error")

def ds18b20_writeByte(dat):
i=0
tmep=dat
for i in range(8):
pin8.write_digital(0)
pin8.write_digital(dat & 0x01)
pin8.write_digital(1)
dat>>=1

def ds18b20_readByte():
i=0
value=0
for i in range(8):
pin8.write_digital(0)
value>>=1
pin8.write_digital(1)
if pin8.read_digital()==0:
value|=0x80
DS18B20_Delayus(1)
return value

def ds18b20_readTemperaData():
temp=0
if ds18b20_init():
ds18b20_writeByte(0xcc)
ds18b20_writeByte(0x44)
if ds18b20_init():
ds18b20_writeByte(0xcc)
ds18b20_writeByte(0xbe)
temp=ds18b20_readByte()
temp |= ds18b20_readByte()<<8
temp &= 0x0FFF
else :
print("init error 1")
temp=0x2000
else :
print("init error 2")
temp=0x1000
return temp


while 1 :
print('%d'%ds18b20_readTemperaData())
DS18B20_Delayus(1000)
---------------------------------------------------------------------------
But I get the error "ds18b20_readTemperaData()" number
==========================
0
0
init error 2
4096
0
0
0
0
0
init error 1
8192
0
0
==========================

please who can find how can I change my error code or give a right program with python code

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: DS18B20 ONE-WIRE BBC micro: bit

Post by SpotlightKid » Thu Dec 07, 2017 6:23 pm

Expecting us to debug your code usually doesn't lead to many helpful answers.

I would first start by cleaning up the coding style (tip: use autopep8), then remove the obvious logical errors (e.g. print statements after return), then making proper use of the micro:bit MicroPython API (e.g. sleep).

Also, the micro:bit may just not be fast enough, to bit-bang the onewire protocol in Python.

DRAgon734235
Posts: 2
Joined: Thu Dec 07, 2017 8:10 am

Re: DS18B20 ONE-WIRE BBC micro: bit

Post by DRAgon734235 » Fri Dec 08, 2017 5:48 am

please give a help

about how to driver the ds18b20 use a bbc micro : bit

Post Reply