-1

i am using 3.7~4.2V lithium battery. I am using internal voltage reference to read constant battery voltage as we know that the battery voltage level depletes overtime. The problem is that my sensor (mini solar panel) reads max value under little light and does not go beyond that level no matter how much light falls onto it in the later stage. I need my logic to be <<< if read sensor voltage less than 3V detect night and do something whereas if voltage level is above 3V detect day and go to sleep. The entire project is ready i just need to figure out this.

void setup(){
 Serial.begin(9600);
 pinMode (A0, INPUT);
 pinMode (A1, INPUT);
}

void loop() {  
 printVolts();
  //REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
  analogReference( INTERNAL);
  //We read A1 (MUX0)
  ADMUX = 0b00000000;   
  DIDR0 = 0;  
  // Start AD conversion
  ADCSRA |= (1<<ADSC);
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
  val = ADCL | (ADCH << 8);
  val = val * 5.7; //Multiply by the inverse of the divider
  Serial.println("val:     ");
  Serial.println(val); 
}    
void printVolts()
{
  int sensorValue = analogRead(A1);
  voltage =  (sensorValue/ val) * 1023.; 

delay(1000);
Serial.println(                    "voltage:  ");
Serial.print(voltage); 
 }

Currently i am using arduino board as a prototype, later on, i will be using attiny IC.

//////////////////////////////////edit1//////////////////////////////

Circuit

////////////////////////Reads max under ceiling light Reads max under ceiling light

////////////////////////////////////Voltage after covering full panel with hand Voltage after covering full panel with hand

//////////////////////////edit2///////////////////////////////////////////

#include <avr/io.h>
float val;
float voltage;
int led = 8;
void setup(){
 Serial.begin(9600);
 pinMode (A0, INPUT);
 pinMode (A1, INPUT);
 pinMode (led, OUTPUT);

}

void loop() {  
 printVolts();
  //REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
    ADMUX = _BV(REFS0)  | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  //We read A1 (MUX0)
  ADMUX = 0b00000000;   
  // Start AD conversion
  ADCSRA |= (1<<ADSC);
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
 long result = ADCL | (ADCH << 8);
 val = result;
  val = val * 5.7; //Multiply by the inverse of the divider
  Serial.println("val:     ");
  Serial.println(val);
}
 void printVolts()
{
  int sensorValue = analogRead(A1);
  voltage =  ((sensorValue/ val) * 1023.); 
delay(1000);
Serial.println(                    "voltage:  ");
Serial.print(voltage); 
if (voltage<=199){
    digitalWrite (led, HIGH);
  }
  else if (voltage>=200){
    digitalWrite (led, LOW);
  }
}

This code should work.

//////////////////////////EDIT3////////////////////////////////////////////

ATtiny44A

#include <avr/io.h>
float val;
float voltage;
int led = 3;
#define solar 2 

void setup(){
 Serial.begin(9600);
 pinMode (A0, INPUT);
 pinMode (A1, INPUT);
 pinMode (led, OUTPUT);

}

void loop() {  
 printVolts();
  //REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
     ADMUX = _BV(MUX5) | _BV(MUX0) ;
  //We read A1 (MUX0)
  ADMUX = 0b00000001;   //PA1   
  // Start AD conversion
  ADCSRA |= (1<<ADSC);
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
 val = ADCL | (ADCH << 8);
 //long result = ADCL | (ADCH << 8);
  //val = result;
  val = val * 5.7; //Multiply by the inverse of the divider
  Serial.println("val:     ");
  Serial.println(val);
}
 void printVolts()
{
  int sensorValue = analogRead(2); //PA2
  voltage =  (sensorValue/ val) * 1023.; 

delay(1000);
Serial.println(                    "voltage:  ");
Serial.print(voltage); 
if (voltage<=214){
    digitalWrite (led, HIGH);
  }
  else if (voltage>=214){
    digitalWrite (led, LOW);
  }
}

///////////////////////////EDIT//////////////////////////////////////// VALUE WITH analogReference(INTERNAL) command.

179.47val:
5831.10 voltage:
179.47val:
5831.10 voltage:
179.47val:
5831.10 voltage:
179.47val:
5831.10 voltage:
179.47val:
5831.10 voltage:
179.47val:

it maxes out under little light and cannot go beyond this level.

 ADMUX |=  (0<<REFS0) |(1<<REFS1);
 ADMUX = _BV(MUX5) | _BV(MUX0) ;

if I use these commands then serial monitor shows:

voltage:
132.47val:
3357.30 voltage:
infval:
3334.50 voltage:
132.23val:
3345.90 voltage:
133.00val:
3351.60

low reading

voltage:
8.24val:
3351.60 voltage:
7.94val:
3351.60 voltage:
7.63val:
3351.60 voltage:
9.46val:
3351.60

High reading

voltage:
183.75val:
3357.30 voltage:
178.86val:
3357.30 voltage:
181.00val:
3357.30 voltage:
181.61val:
3351.60

1
  • Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on Arduino Meta, or in Arduino Chat. Comments continuing discussion may be removed.
    – Juraj
    Feb 23 at 12:38

1 Answer 1

1

The problem is that my sensor (mini solar panel) reads max value under little light and does not go beyond that level no matter how much light falls onto it in the later stage.

Open voltage is not a good way to read the light level hitting a solar panel in this situation. Much better is to measure the current generated by the solar cell by adding a resistor across it here...

enter image description here

The value of the resistor can be found using...

V=IR

... where ...

V=1.1 volts

I=the maximum current the solar cell can generate in full light

If you do this, then you can read the A1 pin using the 1.1V ref and you will be able to measure the full range of light conditions using the solar cell with simple Arduino code like this..

analogReference(INTERNAL);  // a built-in reference, equal to 1.1 volts 
int v=analogRead(A1);       // Returns value 0-1023 representing light level

PS: Note if you measure the battery voltage with a divider as shown, you will be drawing current (about 0.8 milliamps when the battery reaches its minimum voltage) ALL THE TIME - even when the circuit is off. You do not want to do this with a lithium battery since under-voltaging it can potentially cause permanent damage to it. You really should heed my answer to your other question here.

PPS: It is usually best to ask your real question directly rather than asking a bunch of questions that are synthetic and reflect problems you think you have rather than the problems you actually do have. Your supplied code and drawings should match the thing you are asking and the thing you are actually working on (and each other!). It is often the case that the problem is not where you think it is, and so showing code and drawings that do not reflect what is really going on just wastes people's effort and does not ultimately help you solve your real problem. :)

16
  • Then how do i reduce my battery voltage to Vref? i read another way to do so is to change the resistor value to 47k and 10k, in that manner they'll be consuming 65uA. If i connect resistor on the solar end, the current drawn would increase and it would result into slow battery charging which again is not a suitable option for the specific project. Feb 22 at 9:02
  • "Then how do i reduce my battery voltage to Vref?" The best way to measure the voltage of your power source is by measuring the 1.1V ref against Vcc as suggested in my answer to your other question and the article linked there.
    – bigjosh
    Feb 22 at 9:05
  • " If i connect resistor on the solar end, the current drawn would increase and it would result into slow battery charging which again is not a suitable option for the specific project. " You question is about using a solar cell as a sensor to measure the light level. This answer tells you a good way to do that. If you are using the solar cell to charge the battery, then this is not represented in the question, in the code, or in the drawing so it is hard for us to help you with that! :) And note that charging a battery is a very different thing than using it as a light sensor.
    – bigjosh
    Feb 22 at 9:08
  • 1
    These are all very different things that what is asked in this question and your others. Do note that charging a lithium battery is non-trivial and is not as easy as just connecting a solar cell to it. Check out batteryuniversity.com/article/bu-409-charging-lithium-ion for some info.
    – bigjosh
    Feb 22 at 9:22
  • 1
    Your code above has many problems and does not compile. Best way to solve this problem (or any problem) is to find the minimum code and circuit that reproduces the problem and post that, along with (1) what you want to have happen, (2) what actually happens with the supplied code and circuit. For me, very often just systematically following these steps will actually end up with me solving the problem myself - but if not then at least you make it easy for other people to help you when you post it.
    – bigjosh
    Feb 22 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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