Hello Arduino community, I would like to ask you for help in solving my problem. I made some soft capacitive pressure sensors and I need to read their capacitance value using Arduino and the CDC AD7746. By making the various connections and writing the sketch (it doesn't give me errors in the compilation), the same capacity (-4,096 pF) always appears on the serial monitor both when connecting the sensor and when connecting only the AD7746. Can any of you tell me how I can solve it? Thank you
Nobody is going to be able to help you with the limited information you provided.
Please post your code, in code tags:
![]()
Please also post a schematic showing your wiring and tell us which board you are using.
See this link for info on how to use the forum
thank you and sorry. This is my first time using the forum. My goal is to read the capacitance values of a soft capacitive sensor made by me using the Arduino Uno board and the CDC AD7746 component. I wrote the following code but when I send it to compile it always gives me the same value (photo of the serial monitor attached) and this happens both when I read only the value of the AD7746 and when I also add the capacitive sensor.
#include <Wire.h>
#define I2C_ADDRESS 0x48
#define REGISTER_STATUS 0x00
#define REGISTER_CAP_DATA 0x01
#define REGISTER_CAP_SETUP 0x07
#define REGISTER_EXC_SETUP 0x09
#define REGISTER_CONFIGURATION 0x0A
#define REGISTER_CAP_OFFSET 0x0D
#define REGISTER_CAP_DAC_A 0x0B
void setup() {
Serial.begin(9600);
Serial.println(" Inizializzazione...");
Wire.begin();
// **RESET AD7746**
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(0xBF); // Comando di reset
Wire.endTransmission();
delay(2000);
// **Scansione I2C per verificare se il chip è riconosciuto**
Serial.println("Scansione I2C...");
Wire.beginTransmission(I2C_ADDRESS);
if (Wire.endTransmission() != 0) {
Serial.println(" Errore: AD7746 non trovato!");
while (1);
}
Serial.println("AD7746 trovato all'indirizzo 0x48");
// **Attiva l'eccitazione EXC_A per il sensore**
writeRegister(REGISTER_EXC_SETUP, _BV(2) | _BV(1) | _BV(0)); // Abilita EXC_A
// **Imposta il canale CIN1 per la capacità**
//writeRegister(REGISTER_CAP_SETUP, _BV(7));
writeRegister(REGISTER_CAP_SETUP, _BV(7) | _BV(6)); // Abilita la misura differenziale
// **Attiva la conversione**
writeRegister(REGISTER_CONFIGURATION, _BV(7) | _BV(6) | _BV(5) | _BV(4) | _BV(3) | _BV(2) | _BV(0));
delay(1000);
// **Legge e stampa l'offset**
long offset = readLong(REGISTER_CAP_OFFSET);
Serial.print(" Offset di fabbrica: ");
Serial.println(offset);
// **Forza il DAC per calibrazione**
writeRegister(REGISTER_CAP_DAC_A, _BV(7) | 100);
Serial.println(" Configurazione completata!");
}
void loop() {
uint8_t status = readRegister(REGISTER_STATUS);
Serial.print(" Registro di stato: ");
Serial.println(status, BIN);
if (!(status & _BV(0))) {
Serial.println(" Dato non pronto!");
delay(500);
return;
}
long rawValue = readValue();
if (rawValue == -1) return;
float capacitance_pF = convertToPicoFarads(rawValue);
Serial.print(" Valore Grezzo: ");
Serial.print(rawValue);
Serial.print(" | Capacità: ");
Serial.print(capacitance_pF, 6);
Serial.println(" pF");
delay(1000);
}
long readValue() {
int timeout = 1000;
while (!(readRegister(REGISTER_STATUS) & _BV(0)) && timeout > 0) {
delay(1);
timeout--;
}
if (timeout == 0) {
Serial.println(" Timeout nella lettura dello stato!");
return -1;
}
return readLong(REGISTER_CAP_DATA) >> 8;
}
float convertToPicoFarads(long rawValue) {
return ((float)(rawValue - 0x800000) / 0x7FFFFF) * 4.096;
}
unsigned long readLong(unsigned char r) {
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(r);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 3);
if (Wire.available() < 3) {
Serial.println(" Timeout nella lettura I2C!");
return 0;
}
unsigned long value = 0;
for (int i = 2; i >= 0; i--) {
value |= (Wire.read() << (8 * i));
}
return value;
}
void writeRegister(uint8_t reg, uint8_t value) {
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
uint8_t readRegister(uint8_t reg) {
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 1);
return Wire.available() ? Wire.read() : 0;
}
Thank you for posting the code.
I tried running this on an Uno R3
void setup() {
Serial.begin(115200);
byte values1[3] = {0xAA, 0x55, 0x22};
Serial.println(readLong(values1), HEX);
}
unsigned long readLong(byte* byteValues) {
unsigned long value = 0;
for (int i = 2; i >= 0; i--) {
value |= (byteValues[i] << (8 * i));
}
return value;
}
void loop() {
}
The serial monitor output is:
55AA
If I run the same code on an ESP32, the serial monitor output is:
2255AA
[Edit: Added modification of code to get expected value on Uno R3]
value |= ((unsigned long)byteValues[i] << (8 * i));
Casting the byteValues to unsigned long produces the expected output on the Uno R3's serial monitor:
2255AA
@psusa99 I don't know if there is anything else wrong with the code. I don't know anything about AD7746, other than a quick look at its data sheet when I was reading your first post. Hopefully getting the correct value from readLong() will be a step in the right direction. If you still have problems after fixing that, then report the results back here and someone may be able to help some more.
