Hello. I have a problem with a capacitive sensor. I want to read it with i2c and can't get any further with the following code. Can someone help me?
#include <Wire.h>
// I2C-Adresse des LCPB-Sensors
#define SENSOR_ADDRESS 0x33
// Register-Adresse für capaTEC2
#define CAPATEC2_REGISTER 0x00
void setup() {
// I2C-Kommunikation starten
Wire.begin();
// Serielle Kommunikation für Debugging starten
Serial.begin(9600);
}
void loop() {
// capaTEC2-Wert lesen
int32_t capaTEC2 = readCapaTEC2();
// Wert auf dem seriellen Monitor ausgeben
Serial.print("capaTEC2: ");
Serial.println(capaTEC2);
// Verzögerung vor der nächsten Ablesung
delay(1000);
}
int32_t readCapaTEC2() {
Wire.beginTransmission(SENSOR_ADDRESS);
Wire.write(CAPATEC2_REGISTER);
Wire.endTransmission();
Wire.requestFrom(SENSOR_ADDRESS, 4);
int32_t value = 0;
if (Wire.available() == 4) {
value = Wire.read();
value |= (Wire.read() << 8);
value |= (Wire.read() << 16);
value |= (Wire.read() << 24);
}
return value;
}
Koepel
July 23, 2024, 7:24am
2
Welcome to the forum.
Which Arduino board do you use. Do you have another sensor to check if the I2C bus works ?
What is the result of the sketch ? Does it work ?
The datasheet shows a repeated start.
Manufacturer's page: https://www.ebe.de/en/sensors/level-sensors/liquids-solids/
Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in https://forum.arduino.cc/c/using-arduino/uncategorized/184 .
Hi, @leon_20
Welcome to the forum.
Can you please post a link to data/specs of the sensor?
Thanks.. Tom..
jim-p
July 23, 2024, 10:56am
5
@leon_20
I see the problem.
You need to do a repeated start when you read the data.
Try this code:
#include <Wire.h>
// I2C-Adresse des LCPB-Sensors
#define SENSOR_ADDRESS 0x33
// Register-Adresse für capaTEC2
#define CAPATEC2_REGISTER 0x00
void setup() {
// I2C-Kommunikation starten
Wire.begin();
// Serielle Kommunikation für Debugging starten
Serial.begin(9600);
}
void loop() {
// capaTEC2-Wert lesen
int32_t capaTEC2 = readCapaTEC2();
// Wert auf dem seriellen Monitor ausgeben
Serial.print("capaTEC2: ");
Serial.println(capaTEC2);
// Verzögerung vor der nächsten Ablesung
delay(1000);
}
int32_t readCapaTEC2() {
Wire.beginTransmission(SENSOR_ADDRESS);
Wire.write(CAPATEC2_REGISTER);
byte I2CError = Wire.endTransmission(false); //repeated start
if (I2CError == 0)
Serial.println ("OK ");
else
{
Serial.print ("Error ");
Serial.println (I2CError);
}
Wire.requestFrom(SENSOR_ADDRESS, 4, true);
delay(10);
int32_t value = 0;
if (Wire.available() == 4) {
value = Wire.read();
value |= (Wire.read() << 8);
value |= (Wire.read() << 16);
value |= (Wire.read() << 24);
}
return value;
}
I use the Arduino UNO. The sketch doesn´t work
Thanks for the code, but it doesn't work. It looks like the sensor is not communicating with the Arduino UNO. The transmit LED stays off. When I pull VCC out of the Arduino, I also get the error message you programmed in.
It does work, I had not connected any pull-up resistors
jim-p
July 24, 2024, 7:22am
10
Yes, pull-up are always needed for I2C.
Glad it worked
Have a nice day!
Sorry, your code is still working but it only shows values of 256, 512, 768, 1024, 1280, 1536, 1792. the sensor should also show intermediate values. Do you know how ich can do this?
jim-p
July 25, 2024, 7:52am
12
It's your program, I just fixed it so that it correctly reads the I2C bus. The number you see is what the sensor sent.
The data sheet doesn't give much information about how the sensor works or if you need to apply a scale factor, what the LSB is but it looks like the bottom 8 bits are 0.
Try shifting capaTEC2 right 8 bits.
capaTEC2 = capaTEC2 >> 8;
Is there any difference between the two forms of the following commands when the default value for the 3rd argument of requstFrom() method is true ?
Wire.requestFrom(SENSOR_ADDRESS, 4, true); //release I2C Bus
Wire.requestFrom(SENSOR_ADDRESS, 4);
jim-p
July 25, 2024, 8:22am
14
Since the default is true, there is no difference. It added it just to make it clear that a stop bit should be sent.
Is the above statement correct?
requestFrom() method has requested for four bytes data. The method is a blocking code and will be terminated as soon as four bytes data are accumulated in the I2C Serial Buffer.
There is no neead for repeated START to hold the I2C Bus as there is no other I2C Master Device to acquire the bus.
Koepel
July 25, 2024, 8:46am
17
We don't know the digital logic in the sensor. If the datasheet shows a repeated start, then that is what the sensor probably expects.
Although the I2C bus is pretty well standardized, the manufacturers of the chips sometimes make mistakes in the chip logic.
jim-p
July 25, 2024, 8:50am
18
leon_20:
Sorry, your code is still working but it only shows values of 256, 512, 768, 1024, 1280, 1536, 1792. the sensor should also show intermediate values. Do you know how ich can do this?
See post #12
Ignore #13 , 14 ,15 , 16 and 17
system
Closed
January 21, 2025, 8:50am
19
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.