How to read sensor voltage level accurately

(Read 505 times)
How to read sensor voltage level accurately on: March 01, 2023, 07:18:21 AM
Hi there,
i followed Mr.electronoob video on youtube and was able to measure constant battery voltage and that was exactly what i needed at an initial stage. Now, i have to add sensor to my code but after adding sensor to it the reading are not accurate. I am using solar panel for battery charging purpose during the daytime. The solar panel must be able to read voltage levels in order to detect day or night mode. for example: if (solar_voltage>=4){
Do something
}



ok, so heres's my code.


int led = 8;

void setup(){
Serial.begin(9600);
pinMode (led,OUTPUT);
}

void loop() {
//REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
analogReference (INTERNAL);
//ADMUX = B11000000;
//We read A1 (MUX0)
int value = analogRead (A1);
//ADMUX = B00000001;
//int sensor = analogRead(A1);
// Start AD conversion
ADCSRA |= B11000000;
// Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));
float val = ADCL | (ADCH << 8);
val = val * 5.7; //Multiply by the inverse of the divider
value=val;
Serial.println(val);

float voltage = val * analogRead(A0) / 1023;
voltage = ADCL | (ADCH << 8);
Serial.print ("voltage at A0:");
Serial.println(voltage);
Serial.print ("Solar panel voltage:");
Serial.println(voltage * 5.7);
delay(1000);

if (voltage>=174){  //1.1V
digitalWrite (led,HIGH);
}
else{
digitalWrite(led,LOW);
}
}

output was recorded from a Serial monitor. Schematic and output file has been attached below.

The issue is i need to connect my battery to solar panel and if i do so, the solar panel stops working. I believe that only you can better understand my situation as i have followed your tutorial.








Re: How to read sensor voltage level accurately Reply #1 on: March 01, 2023, 10:33:11 AM
DOn't really understand what is the problem. Make sure you use analogReference (INTERNAL); only when needed and when you use that function, the voltage refferance is not 5V anymore, is 1.1V from the internal voltage refferance. I can't understand what is hoind wrong with the rest because I don't know what you are actually trying to do... Sorry!



Re: How to read sensor voltage level accurately Reply #2 on: March 06, 2024, 05:51:33 AM
Obtaining precise information is particularly challenging. Sentence construction and programming play a major role in the issue. An analog input is a field-to-PLC input that is continuous. wherein certain inputs, such as A0 through A5, are used. We can learn more about it.scratch games



Re: How to read sensor voltage level accurately Reply #3 on: April 04, 2024, 11:32:53 AM
From your code, it looks like you're using the internal 1.1V reference for analog readings, which should be suitable for measuring battery and solar panel voltages. However, it seems there might be some discrepancies in your code logic. It's important to ensure that your sensor readings and calculations are accurate.