After several tries, i was successful with reading stable battery voltage using internal voltage reference of 1.1v, now the issue i face is that i cannot read stable sensor voltage, could you please let me know how can i read my sensor voltage? or do i need additional voltage divider (my sensor goes directly to A0 and GND pin).

  void setup(){

void loop() {  
  //REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
  ADMUX |= B11000000;   
  //We read A1 (MUX0) for battery voltage
  ADMUX |= B00000001;       
    //We read A0 for sensor voltage
  ADMUX |= B00000000;                // for my sensor 
  // Start AD conversion
  ADCSRA |= B11000000;
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
  float val = ADCL | (ADCH << 8);
  val = val * 5.7; // Using 4.7k and 1k voltage divider

Something like the code below must workout but unfortunately it does not help!

  int sensorValue = analogRead(A0);
  voltage =  ((sensorValue/ val) * 1023.); 
  Serial.println("voltage:  ");
  if (voltage<=215){
  digitalWrite (led, HIGH);
  else if (voltage>=215){
  digitalWrite (led, LOW);

I cannot find the issue, it should work, i believe.

//////////////////////new edit/////////////////////////////////////

const long internalReference = 1078L; //measured AREF to be 1.078 ~ 1.078*1000= 1078V

void setup() {
  // put your setup code here, to run once:
Serial.begin (115200);

void loop() {
 analogReference (INTERNAL);
 ADMUX |= B00000000;
 ADCSRA |= B11000000;    //ADEN and ADSC equal to 1   (Start conversion)
  while(bit_is_set(ADCSRA, ADSC));    //Wait for conversion to end
  long val = ADCL | (ADCH << 8);   //Get analog read value  
  float voltage = (val/internalReference)*1024;
 Serial.println (voltage);

enter image description here

  • 2
    Looking at your past posts, you are repeatedly using the wrong code and then asking why it doesn't work. Maybe you should search how the ADC work. The reading from sensorValue = analogRead(A0) is a value ranging from 0 to 1023, to convert it to a voltage, the formula is voltage = (sensorValue/Vref) * 1023, where Vref is the reference Voltage you are using, in this case, it is 1.1v. Furthermore, you can't get a correct sensorValue if your input at A0 is HIGHER than the Vref, you will get the wrong value!
    – hcheung
    Feb 28 at 5:10
  • @hcheung ok,understood. i will change now and update. before i do so please ans my question: 1) if i use vref as 1.1v, do i need to measure my battery voltage, will it affect the code in any way? because my system only requires sensor measurement that's it. i am following this link (electronoobs.com/eng_arduino_tut156.php) that says we need to measure battery voltage then we can use it with the sensor code. i will update you with the results please stay connected Feb 28 at 5:22
  • 2
    Whether you want to measure and calibrate the Vref, it is +/-10% differences according to datasheet (I mentioned this already on my prefer answer, please read them). so that means the Vref could be in the range of 0.99 - 1.21v.
    – hcheung
    Feb 28 at 5:28
  • @hcheung Hi, please check the code above it displayed constant 0.00V. i have connected solar to a voltage divider that drops the voltage down to 0.85V. Feb 28 at 5:53
  • In the schematic above the LDR has been used in place solar panel due to the unavailability of the component in the software. Feb 28 at 5:59

1 Answer 1


To measure the voltage at A0 with an internal reference of 1.1v, the formula is float voltage = 1.1 * analogRed(A0) / 1023;. Your code can be simplified as

float Vref = 1.1;

void setup(){

void loop() {
  analogReference (INTERNAL);
  float voltage = Vref * analogRead(A0) / 1023;
  Serial.print (“voltage at A0:”);
  Serial.print (“Solar panel voltage:”);
  Serial.println(voltage * 5.7);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.