Hi,
I want to read the ADC values of a PCF8591 module like here:
https://sunupradana.info/tkr/category/embedded-systems/arduino/
With the code
#include "Wire.h"
#define PCF8591 (0x90 >> 1) //0x48
byte adcvalue0, adcvalue1, adcvalue2, adcvalue3;
void setup()
{
Wire.begin();
Serial.begin(115200);
}
void loop()
{
Wire.beginTransmission(PCF8591);
Wire.write(0x04); // SDA // PC4
Wire.endTransmission();
Wire.requestFrom(PCF8591, 5); //SCL //PC5
adcvalue0=Wire.read();
adcvalue0=Wire.read();
adcvalue1=Wire.read();
adcvalue2=Wire.read();
adcvalue3=Wire.read();
Serial.print(adcvalue0);
Serial.print(" ,");
Serial.print(adcvalue1);
Serial.print(" ,");
Serial.print(adcvalue2);
Serial.print(" ,");
Serial.print(adcvalue3);
Serial.println();
delay(1000);
}
The first ADC input is grounded. When I run the code I get results like this:
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,216 ,219 ,148
215 ,219 ,147 ,0
215 ,215 ,219 ,149
0 ,215 ,219 ,148
0 ,215 ,219 ,148
215 ,219 ,148 ,0
215 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
215 ,219 ,148 ,0
215 ,215 ,219 ,148
215 ,219 ,148 ,0
215 ,219 ,148 ,0
215 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,220 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,148
0 ,215 ,219 ,147
0 ,216 ,219 ,149
0 ,216 ,219 ,148
0 ,217 ,219 ,148
0 ,217 ,219 ,148
217 ,219 ,148 ,0
217 ,217 ,220 ,148
0 ,217 ,219 ,148
0 ,216 ,219 ,148
0 ,216 ,219 ,148
0 ,216 ,219 ,148
216 ,219 ,148 ,0
216 ,217 ,219 ,148
0 ,217 ,219 ,148
0 ,217 ,219 ,148
0 ,217 ,219 ,148
It seems to me like the first register is sometimes skipped or missed.
I tried different variants like
byte readADCIn(byte pin){
Wire.beginTransmission(PCF8591); // wake up PCF8591
Wire.write(pin); // control byte - read ADC2
Wire.endTransmission(); // end tranmission
Wire.requestFrom(PCF8591, 2);
Wire.read();
byte value=Wire.read();
Serial.print("Pin ");
Serial.print(pin);
Serial.print(" read: ");
Serial.println(value);
}
void update(){
for(byte pin=0; pin<4; pin++){
readADCIn(pin);
delay(1000);
}
...
or
...
byte count = 0;
while (Wire.available()) {
byte val = Wire.read();
Serial.print(count++);
Serial.print(" read: ");
Serial.println(val);
}
...
but the results are the same, even if I try to read a specific register, sometimes I read different values.
I'm using an Adafruit HUZZAH board.
Any idea what's going wrong here?
Best regards Charly