Ultrasonic Sensor not working

Hi, I'm trying to make a ultrasonic sensor and using MB1242-000. MB1242-000 is connected to Arduino Uno and measure the distance from the ground. I wrote a program referring to the data sheet by uaing Arduino IDE.(https://www.mouser.com/datasheet/2/830/I2CXL_MaxSonar_EZ_Datasheet-1534886.pdf#:~:text=This%20is%20the%20datasheet%20for%20the%20I2CXL-MaxSonar-EZ%20ultrasonic,of%20the%20MaxSonar%20high%20performance%20sonar%20range%20finders.)
However, this program didn't work and display on serial monitor. If you find any mistakes please let me know. Help would be appreciated, thanks! Here is my code:

// Pin settings for I2C communication
#define SCL_PIN 5            // SCL (clock) pin
#define SCL_PORT PORTC
#define SDA_PIN 4            // SDA (data) pin
#define SDA_PORT PORTC
#define I2C_TIMEOUT 100      // Timeout setting (100ms)
#include <SoftI2CMaster.h>  // I2C communication library
// Sensor address (default)
#define SENSOR_ADDRESS 224   // Default address of the sensor to use

void setup() {
  // Start serial communication
  Serial.begin(9600); 
  i2c_init();  // Initialize I2C communication

  Serial.println("Setup complete!");
}

void loop() {
  // Read distance from the sensor
  read_the_sensor_example();
  
  delay(1000);  // Wait 1 second before reading again
}

void read_the_sensor_example(){ 
  boolean error = 0;  // Create a bit to check for catch errors as needed. 
  int range; 
   
  // Take a range reading at the default address of 224 
  error = start_sensor(224);    // Start the sensor and collect any error codes. 
  if (!error){                  // If you had an error starting the sensor there is little point in reading it 
  
    delay(100); 
    range = read_sensor(224);   // Reading the sensor will return an integer value -- if this value is 0 there was 
    
    Serial.print("R:");Serial.println(range); 
  } 
}

int read_sensor(byte bit8address){ 
  boolean errorlevel = 0; 
  int range = 0; 
  byte range_highbyte = 0; 
  byte range_lowbyte = 0; 
  bit8address = bit8address | B00000001;  // Do a bitwise 'or' operation to force the last bit to be 'one' -- we are 
  errorlevel = !i2c_start(bit8address) | errorlevel; 
  range_highbyte = i2c_read(0);           // Read a byte and send an ACK (acknowledge) 
  range_lowbyte  = i2c_read(1);           // Read a byte and send a NACK to terminate the transmission 
  i2c_stop(); 
  range = (range_highbyte * 256) + range_lowbyte;  // Compile the range integer from the two bytes received. 
  if(errorlevel){ 
    return 0; 
  } 
  else{ 
    return range; 
  } 
}

boolean start_sensor(byte bit8address){ 
  boolean errorlevel = 0; 
  bit8address = bit8address & B11111110;               // Do a bitwise 'and' operation to force the last bit to be
 
  errorlevel = !i2c_start(bit8address) | errorlevel;   // Run i2c_start(address) while doing so, collect any errors 
  errorlevel = !i2c_write(81) | errorlevel;            // Send the 'take range reading' command. (notice how the  
  
  i2c_stop(); 
  return errorlevel; 
}

hi @akare8787, welcome to the forum.

Google for an "i2c scanner arduino".

You will find simple code to verify that your sensor is on the bus.

I'm in transit otherwise I'd do it for you.

a7

define them as A4 and A5 (and wire accordingly).

This bus is 0x70. No worries at all, I really appreciate your willingness to help.

Thank you for your advice! I’ve already wired them to A4 and A5 as suggested, so the wiring should be correct.

If you are using softI2C there is no need to change pins,
However you must tell softI2c which pins you are using

SoftI2CMaster i2c = SoftI2CMaster( sclPin, sdaPin, 0 );

So then define them as A4 and A5 as well....

and the address is 0xE0

Use commands like this

 error = i2c.beginTransmission(address);
 i2c.endTransmission();

OK, so you are sure that the device is wired correctly and you e seen it on the bus using code you did not write it mess with, a scanner or test program of some kind?

Also, why are you using software I2C? It can't be because you have other planz for A4 and A5, the hardware I2C pins.

a7

Is there a reason why you are using softI2Cmaster and not the Wire library?

Your have a conflict (0x70 = DEC 112, 0xE0 = DEC 224). Run this.

#include <Wire.h>
int address, deviceFound,  error;

void setup() {
  Wire.begin();
  Serial.begin(115200);
  Serial.print("I2C bus scan:");

  for (address = 0; address < 128; address++ )  {
    Wire.beginTransmission(address);
    if (!Wire.endTransmission()) {
      showHex(address);
      deviceFound++;
    }
    else if (error == 4) {
      Serial.print(" Error at ");
      showHex(address);
    }
  }
  if (!deviceFound)
    Serial.println(" No I2C device found.");
}

void loop() {}

void showHex(int hex) {
  Serial.print(" 0x");
  if (hex < 0x10)
    Serial.print(0);
  Serial.print(hex, HEX);
}

That's the I2C address and half of it or twice it.

I can never remember if you supposed to use 0x70 or 0xE0, that is to say who drops the bottom bit?

Maybe she did run a scanner as has been suggested. Thanks @xfpd for posting that and reducing the hurdle it may have been keeping her from doing.

a7

Asking for me (because my friend is shy), is the "half/double" address the silkscreen on the board/docs or the scanned address, and how is it interpreted (when should I know to use another value than the result of the scan). I don't even know if I am asking the right question.

WOKWI: I played with I2C addresses on Wokwi and fit a bunch of devices with the same address by giving them different "attrs": { "i2cAddress": "0x??"} in diagram.json. Two devices on Wokwi have the same address (LCD and IMU?) as a default.

Nice idea with the wokwi. When you specify that attribute, is it the number you needa use in constructors or begin() methods?

Did you try the scanner in wokwi? Does it find the device at that same number? I would except I'm at the beach on a tablet, I'm too lazy to fight with wokwi, especially anonymously as it would have to be.

I wish I could hang onto this, I have to "learn" it every time, obvsly I am not learning. :expressionless:

a7

The attribute goes in the diagram.json... a few of the I2C components have the setting on the "datasheet" page.

Yes "tried this in wokwi" and yes "finds the same number/address"... but does not print out "two devices."

I gave similar devices sequential addresses. When I return, I will see if I can use the devices independently.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.