Hey there!
Having a hard time with my TOF10120 sensor and the arduino nano. I tried the same sensor to an arudino UNO and it works. Heres my code :
#include <Wire.h>
#define TOF10120_ADDRESS 0x52 // Default I2C address for TOF10120
void setup() {
Wire.begin(); // Initializes I2C communication on SDA (A4) and SCL (A5)
Serial.begin(9600); // Initializes serial communication at 9600 baud rate
Serial.println("TOF10120 Distance Measurement");
}
void loop() {
uint16_t distance = readTOF10120();
if (distance != 0) {
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" mm");
} else {
Serial.println("Failed to read distance.");
}
delay(500); // Wait for 500 ms before taking another reading
}
uint16_t readTOF10120() {
Wire.beginTransmission(TOF10120_ADDRESS);
Wire.write(0x00); // Write the register address from where to start reading
Wire.endTransmission();
Wire.requestFrom(TOF10120_ADDRESS, 2); // Request 2 bytes from the sensor
if (Wire.available() == 2) {
uint8_t highByte = Wire.read();
uint8_t lowByte = Wire.read();
uint16_t distance = (highByte << 8) | lowByte; // Combine the two bytes to form the distance value
return distance;
} else {
return 0; // Return 0 if the reading was not successful
}
}
But when I'm trying to run the same code onto my nano, it doesn't detect anything. All my connections are the same onto my nano than my uno (a4 (SCL green wire), a5(SDA blue wire), 5v (VCC red wire) and GND (ground black wire), but it doesnt detect my sensor I confirmed with this code.
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
} else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000);
}
Is there a problem with the address of the sensor maybe?I tried on multiple nano sensors. Im not sure what to do more :')
Thank a lot,
Mérédith