Speedy2k
Posts: 4
Joined: Sat Apr 13, 2013 7:26 pm

Arduino Code to Python for i2c sensor help!

Fri Dec 06, 2013 8:03 pm

Hi, i want to use this sensor on our project:
http://www.maxbotix.com/Ultrasonic_Sensors/MB7040.htm
They have a sample arduino wire library to use this sensor:

Code: Select all

Arduino I2C Code Example for I2CXL-MaxSonar Products
Development System - Arduino Uno (as of Arduino 1.0.1)
Develop. Board:     Arduino Uno R3

/* Code for Arduino Uno R3
Assumes the sensor is using the default address
Sensor Connections:
Pin 7 to GND 
Pin 6 to 5V
Pin 5 to SCL
Pin 4 to SDA
Requires pull‑ups for SCL and SDA connected to 5V to work reliably
*/
#include "Wire.h"
//The Arduino Wire library uses the 7-bit version of the address, so the code example uses 0x70 instead of the 8‑bit 0xE0
#define SensorAddress byte(0x70)
//The Sensor ranging command has a value of 0x51
#define RangeCommand byte(0x51)
//These are the two commands that need to be sent in sequence to change the sensor address
#define ChangeAddressCommand1 byte(0xAA)
#define ChangeAddressCommand2 byte(0xA5)

void setup() {
        Serial.begin(9600);//Open serial connection at 9600 baud
        Wire.begin();//Initiate Wire library for I2C communications with I2CXL‑MaxSonar‑EZ
}

void loop() {
        takeRangeReading();                                       //Tell the sensor to perform a ranging cycle
        delay(100);                                                    //Wait for the sensor to finish
        word range = requestRange();                           //Get the range from the sensor
        Serial.print("Range:");Serial.println(range);          //Print to the user
}

//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 
    }
}

/* Commands a sensor at oldAddress to change its address to newAddress 
oldAddress must be the 7-bit form of the address that is used by Wire 
7BitHuh determines whether newAddress is given as the new 7 bit version or the 8 bit version of the address 
\ If true, if is the 7 bit version, if false, it is the 8 bit version 
*/
void changeAddress(byte oldAddress, byte newAddress, boolean SevenBitHuh){ 
       Wire.beginTransmission(oldAddress);                 //Begin addressing
       Wire.write(ChangeAddressCommand1);              //Send first change address command
       Wire.write(ChangeAddressCommand2);              //Send second change address command 
 
       byte temp;
       if(SevenBitHuh){ temp = newAddress << 1; }     //The new address must be written to the sensor
       else     { temp = newAddress;         }               //in the 8bit form, so this handles automatic shifting
       Wire.write(temp);                                          //Send the new address to change to 
       Wire.endTransmission();
}

As a minor note, in old Arduino IDE's
Wire.read() should be substituted by Wire.receive()
and
Wire.write() should be substituted by Wire.send()
Based on what i see there, i am trying to to take a distance reading in python with smbus.
As of now, i ear the sensor taking a reading, but i can not read back any value from the sensor.
Here is what i got so far, a little help would be really appreciated!

Code: Select all

#!/usr/bin/python
import time
import smbus
bus = smbus.SMBus(1)
addr = 0x70
try:
    bus.write_byte(addr, 0x51)
except IOError, err:
    print err
time.sleep(1)
try:
    HighByte = bus.read_byte(addr)
    LowByte = bus.read_byte(addr)
except IOError, err:
    print err
print HighByte
print LowByte
Everything i read from bus.read_byte(addr) is 0

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Arduino Code to Python for i2c sensor help!

Sat Dec 07, 2013 7:08 am

Have a look here and check everything is working right before running your script
http://www.raspberry-projects.com/pi/pr ... nterface-2

Dave
Apple say... Monkey do !!

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Arduino Code to Python for i2c sensor help!

Sat Dec 07, 2013 12:17 pm

Code: Select all

//The Arduino Wire library uses the 7-bit version of the address, so the code example uses 0x70 instead of the 8‑bit 0xE0

Code: Select all

addr = 0x70
Have you used 'i2cdetect -y 1' to find the I²C address for your device?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Python”