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//////////////////////////////
////////////////////////Reads max under ceiling light
////////////////////////////////////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