Hi, I have a code that uses 1.1Vref (because the system is powered by battery) (ATTINY44A IC). I have connected one sensor to it and now i would like to read value of the sensor. Please check the code below.
This code uses internal reference voltage and outputs unchanged voltage rating.(THE code is for arduino and with little changes it will work for attiny)
void loop() {
//REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
ADMUX |= B11000000; //We read A1 (MUX0)
ADMUX |= B00000001; // Start AD conversion
ADCSRA |= B11000000; // Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));
float val = ADCL | (ADCH << 8);
val = val * 5.7; Serial.println(val); }
from my perspective, in order to read sensor value the code should be:
int sensorVoltage = analogRead (A0);
val = (val * 5.7); val = (val/1024);
val = (val*sensorVoltage);
if (voltage<=2000){ //3V
DO SOMETHING
}
Unfortunately, the above code does not work as needed.
for example, if the sensor at 3v reads 2000 then it should be something like if (sensorvalue<=2000){do something};
But what if with some other battery little less or little more charged, the sensor does not read 2000 or reads some value greater than 2000 at 3v then what is the solution to it?
The value changes with battery voltage.
The code works alright with arduino but when i transfer it to attiny it does not work properly for instance, arduino reads 2000 at 3v whereas, attiny does not read 2000 at 3-3.4V.
This code uses internal reference voltage and outputs unchanged voltage rating.(THE code is for arduino and with little changes it will work for attiny)
void loop() {
//REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
ADMUX |= B11000000; //We read A1 (MUX0)
ADMUX |= B00000001; // Start AD conversion
ADCSRA |= B11000000; // Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));
float val = ADCL | (ADCH << 8);
val = val * 5.7; Serial.println(val); }
from my perspective, in order to read sensor value the code should be:
int sensorVoltage = analogRead (A0);
val = (val * 5.7); val = (val/1024);
val = (val*sensorVoltage);
if (voltage<=2000){ //3V
DO SOMETHING
}
Unfortunately, the above code does not work as needed.
for example, if the sensor at 3v reads 2000 then it should be something like if (sensorvalue<=2000){do something};
But what if with some other battery little less or little more charged, the sensor does not read 2000 or reads some value greater than 2000 at 3v then what is the solution to it?
The value changes with battery voltage.
The code works alright with arduino but when i transfer it to attiny it does not work properly for instance, arduino reads 2000 at 3v whereas, attiny does not read 2000 at 3-3.4V.