Borrar filtros
Borrar filtros

Interfacing Ultrasonic Module (MB1242) with Arduino or RPi using I2C protocol

3 visualizaciones (últimos 30 días)
I am trying to interface an ultrasonic module (MB1242) using I2C with Arduino or Raspberry Pi on Simulink. There are two examples (both for Arduino and RPi) on Matlab/Simulink to interface with TM102 temperature sensor using I2C. I bought a TMP102 temperature sensor which communicates with Simulink using I2C and it works perfectly. I have been working on Ardunio mostly because I am more comfortable with it than RPi but eventually I would want this to work on RPi. The Arduino code I have used to test the ultrasonic sensor is as follows;
#include "Wire.h"
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
Wire.beginTransmission(0x70); // Slave address
Wire.write(0x51);
Wire.endTransmission();
delay(100);
Wire.requestFrom(0x70, byte(2));
if (Wire.available() >=2)
{
byte HighByte = Wire.read();
byte LowByte = Wire.read();
word range = word(HighByte, LowByte);
Serial.println(range);
}
}
The main issue I am facing is reading ultrasonic (MB1242) values continuously on Matlab/Simulink. I can only read the last value or as it's written on the datasheet "If the sensor receives a range request after power-up without taking a range reading (i.e. receiving the “Take Range Reading” command), the sensor will respond with two bytes containing information used for in-factory manufacturing purposes".
After getting TMP102 working on Matlab I have modified the code to read the ultrasonic sensor. I can successfully read the value but as soon as I put it in a for loop, Matlab throws an error.
The Matlab code is as follows;
for i = i:50
pause(0.5);
writeRegister(mb1242, 1, hex2dec('51'), 'uint8'); //mb1242 is an object already created with slave address 0x70 (see attached)
data = read(mb1242, 2, 'uint8');
bitconcat(fi(data(1),0,8,0), fi(data(2), 0, 8, 0))
end
The error;
Error occurred while reading data from the I2C device. Check the connections between the hardware and device, clear the device object and create a new one.
Although no error appears in Simulink, as soon as I enter slave value it freezes or gives wrong value. Please see attached.
Any help will be much appreciated.
Thanks you

Respuestas (0)

Categorías

Más información sobre Modeling en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by