Maxbotix Ultrasonic/Ultrasound sensor MB7137 for Lopy
-
Hi,
I have 1 sensor I2C MB7137 : (Datasheet: http://www.m axbotix.com/documents/XL-TrashSonar-WR_Datasheet.pdf)
I need to mesure the distance with this sensor like on arduino:
Arduino code resume:
/* RANGE SENSOR */ #define SensorAddress byte(0x70) // send 112 address #define RangeCommand byte(0x51) // send 81 address void setup(){ configureRangeSensor() } void loop() { takeRangeReading() requestRange() } void configureRangeSensor(){ //sensor: Wire.begin(); delay(100); } //Commands the sensor to take a range reading void takeRangeReading(){ Wire.beginTransmission(SensorAddress); //Start addressing Wire.write(RangeCommand); //send range command Wire.endTransmission(); //Stop and do something else now } //Returns the last range that the sensor determined in its last ranging cycle in centimeters. Returns 0 if there is no communication. word requestRange(){ Wire.requestFrom(SensorAddress, byte(2)); if(Wire.available() >= 2){ //Sensor responded with the two bytes byte HighByte = Wire.read(); //Read the high byte back byte LowByte = Wire.read(); //Read the low byte back word range = word(HighByte, LowByte); //Make a 16-bit word out of the two bytes for the range return range; } else { return word(0); //Else nothing was received, return 0 } }
I need to convert for python for lopy:
My code not work:
from time import sleep_ms from machine import I2C # Default Address MB7137_I2C_DEFAULT_ADDR = 0x70 #112 ADDRESS # Commands CMD_MEASURE_DISTANCE = 0x51 #81 ADDRESS i2c = I2C(0, I2C.MASTER, baudrate=250000) class MB7137(object): print ("start:") def __init__(self, i2c=None): self.i2c = i2c self.addr = MB7137_I2C_DEFAULT_ADDR self.cbuffer = bytearray(2) self.cbuffer[1] = 0x00 def write_command(self, command_byte): print ("write:") self.cbuffer[0] = command_byte self.i2c.writeto(self.addr, self.cbuffer) def readDistance(self): print ("read:") self.write_command(CMD_MEASURE_DISTANCE) sleep_ms(25) dist = self.i2c.readfrom(self.addr, 3) dist2 = dist[0] << 8 dist2 = dist2 | dist[1] return dist sensor = MB7137() sensor.__init__(MB7137_I2C_DEFAULT_ADDR) sensor.write_command() sensor.readDistance()
Help please :)
-
Ok..
Solved:
from machine import I2C
import timeADDR = 0x70
CMD = 0x51configure the I2C bus
i2c = I2C(0, I2C.MASTER, baudrate=250000)
i2c.writeto(ADDR, bytes([CMD]))
time.sleep(0.5)
dist = i2c.readfrom(ADDR, 2)
total = (dist[0]*255)+dist[1]print(total)
-
Olá innovpoint!
I don't have that sensor, but some suggestions:
-
Lower your I2C speed, the sensor is very slow you don't to use a fast speed:
i2c = I2C(0, I2C.MASTER, baudrate=100000)
is enough -
In readDistrance you do sleep_ms(25) but the datasheet recommends waiting 100ms (the Arduino code polls for data, but not sure you have to)
-
In your main code
sensor.write_command()
has no purpose, that's a function used by readDistance
Hope this helps
ps: I formatted up the code in your post, next time please use the forum Markdown syntax using ``` to start/end code blocks
-