How to add sensor to the following code

amishlol234

Joined Mar 10, 2022
32
Hmmm, is the microcontroller directly connected to the battery - no voltage regulator in between?
When you connect the two together, is the panel in darkness or light? What is the solar panel +ve voltage a) before connecting and b) after connecting, in both darkness and light.

Since my system should consume very less current and therefore, i cannot use a voltage regulator.

- The battery is directly connected to IC
- It's in the darkness when I connect it.
- I connected probes to + and - of solar panel, it reads around few mV (400mV something) but when I expose the panel to my phone torch, it reads around 0.8v (with internal reference code).
 

Irving

Joined Jan 30, 2016
5,138
- I connected probes to + and - of solar panel, it reads around few mV (400mV something) but when I expose the panel to my phone torch, it reads around 0.8v (with internal reference code).
This is not connected to battery, measuring at the panel terminals with just the voltage divider connected? Measured using what meter?

With the solar panel on its own, try these two tests. I just want to confirm panel is working OK.
1. What is measured voltage (with multimeter on V range) across its terminals in good sunlight?
2. What is measured current (with multimeter on A range) across its terminals in good sunlight?
 

amishlol234

Joined Mar 10, 2022
32
Try this - Don't mix analogRead and direct register access, you don't know what side effects occur. Keep code clean, don't intermingle stuff, it makes it hard to understand
This tests whether analogRead works on ATTiny44. BTW how are you programming the ATTiny44 and are you using the Arduino IDE?

Code:
int led = 3;
#define solar  A0
#define battery A1

void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode (led, OUTPUT);
pinMode (solar, INPUT);
pinMode(batteryy, INPUT);
analogReference (INTERNAL);
}

void loop() {

  int ADC_A0 = analogRead(solar);
  float A0Voltage = (ADC_A0 *1.1)/1024;
  float solarVoltage = A0Voltage * 5.7;
  Serial.print ("ADC A0 = ");
  Serial.println(ADC_A0 );
  Serial.print ("A0Voltage  = ");
  Serial.println(A0Voltage );
  Serial.print ("solarVoltage  = ");
  Serial.println(solarVoltage );

  int ADC_A1 = analogRead(battery);
  float A1Voltage = (ADC_A1 * 1.1)/1024;
  float batteryVoltage = A1Voltage * 5.7;
  Serial.print ("ADC_A1 = ");
  Serial.println(ADC_A1 );
  Serial.print ("A1Voltage  = ");
  Serial.println(A1Voltage );
  Serial.print ("batteryVoltage = ");
  Serial.println(batteryVoltage );


  delay(1000);

  if (ADC_A0 <=1023){
    digitalWrite (led, HIGH);
  }
  else{
        digitalWrite (led, LOW);

  }
}
i tried the code.

Code:
#include <SoftwareSerial.h>

int led = 7;

#define rx 1

#define tx 0

SoftwareSerial serial (rx,tx);

#define solar 2





void setup() {

// put your setup code here, to run once:

pinMode (rx, INPUT);

pinMode (tx, OUTPUT);

serial.begin (9600);

pinMode (led, OUTPUT);

pinMode (solar, INPUT);

analogReference (INTERNAL);

}



void loop() {



int ADC_A0 = analogRead(solar);

float A0Voltage = (ADC_A0 *1.1)/1023;

float solarVoltage = A0Voltage * 5.7;

serial.print ("ADC A0 = ");

serial.println(ADC_A0 );

serial.print ("A0Voltage = ");

serial.println(A0Voltage );



delay(1000);



if (A0Voltage<=1023){

digitalWrite (led, HIGH);

}

else{

digitalWrite (led, LOW);



}

}

OUTPUT

Code:
A0Voltage = 0.22

ADC A0 = 358

A0Voltage = 0.38

ADC A0 = 391

A0Voltage = 0.42

ADC A0 = 210

A0Voltage = 0.23

ADC A0 = 392

A0Voltage = 0.42

ADC A0 = 393

A0Voltage = 0.42

ADC A0 = 394

A0Voltage = 0.42

ADC A0 = 657

A0Voltage = 0.71

ADC A0 = 995

A0Voltage = 1.07

ADC A0 = 1023

A0Voltage = 1.10

ADC A0 = 1023

A0Voltage = 1.10

ADC A0 = 1023

A0Voltage = 1.10

ADC A0 = 1023

A0Voltage = 1.10

ADC A0 = 1023

A0Voltage = 1.10
under little light (mobile torch) it reads 1023. LED was ON throughout
 

amishlol234

Joined Mar 10, 2022
32
Try this - Don't mix analogRead and direct register access, you don't know what side effects occur. Keep code clean, don't intermingle stuff, it makes it hard to understand
This tests whether analogRead works on ATTiny44. BTW how are you programming the ATTiny44 and are you using the Arduino IDE?

Code:
int led = 3;
#define solar  A0
#define battery A1

void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode (led, OUTPUT);
pinMode (solar, INPUT);
pinMode(batteryy, INPUT);
analogReference (INTERNAL);
}

void loop() {

  int ADC_A0 = analogRead(solar);
  float A0Voltage = (ADC_A0 *1.1)/1024;
  float solarVoltage = A0Voltage * 5.7;
  Serial.print ("ADC A0 = ");
  Serial.println(ADC_A0 );
  Serial.print ("A0Voltage  = ");
  Serial.println(A0Voltage );
  Serial.print ("solarVoltage  = ");
  Serial.println(solarVoltage );

  int ADC_A1 = analogRead(battery);
  float A1Voltage = (ADC_A1 * 1.1)/1024;
  float batteryVoltage = A1Voltage * 5.7;
  Serial.print ("ADC_A1 = ");
  Serial.println(ADC_A1 );
  Serial.print ("A1Voltage  = ");
  Serial.println(A1Voltage );
  Serial.print ("batteryVoltage = ");
  Serial.println(batteryVoltage );


  delay(1000);

  if (ADC_A0 <=1023){
    digitalWrite (led, HIGH);
  }
  else{
        digitalWrite (led, LOW);

  }
}

Code:
#include <SoftwareSerial.h>
int led = 7;
#define rx 1
#define tx 0
SoftwareSerial serial (rx,tx);

void setup() {
  pinMode (rx, INPUT);
  pinMode (tx, OUTPUT);
serial.begin (9600);
pinMode (led, OUTPUT);
analogReference (INTERNAL);
}

void loop() {
float voltage = (analogRead(2)/1023*(1.1)*(5.7));
ADCSRB = (1<<ADLAR);
int ADC_value = ADCL | (ADCH<<8);
serial.print ("solar panel voltage");
 serial.println(ADC_value);
  delay(1000);
 

  if (ADC_value<=1023){
    digitalWrite (led, HIGH);
  }
  else{
        digitalWrite (led, LOW);

  }
}
this code still works by adding this command ADCSRB = (1<<ADLAR); but in opp manner.

solar panel voltage704
solar panel voltage320
solar panel voltage384
solar panel voltage7040
solar panel voltage6272
solar panel voltage8256
solar panel voltage8576
solar panel voltage8576
solar panel voltage8576
solar panel voltage7936
solar panel voltage6336
solar panel voltage28096
solar panel voltage28416
solar panel voltage17856
solar panel voltage0
solar panel voltage0
solar panel voltage320
solar panel voltage0
solar panel voltage0
solar panel voltage0
solar panel voltage1152
solar panel voltage16256
solar panel voltage15872
solar panel voltage15424

led goes off at 0V only rest time it is full ON.


and if i make ADCSRB = (0<<ADLAR);
solar panel voltage21
solar panel voltage11
solar panel voltage79
solar panel voltage19
solar panel voltage118
solar panel voltage112
solar panel voltage88
solar panel voltage131
solar panel voltage113
solar panel voltage95
solar panel voltage132
solar panel voltage110
solar panel voltage106
solar panel voltage31
solar panel voltage0
solar panel voltage1
solar panel voltage13
solar panel voltage0
solar panel voltage23
solar panel voltage52
solar panel voltage145
solar panel voltage181
solar panel voltage153
solar panel voltage118
solar panel voltage1023
solar panel voltage1023
solar panel voltage1023
solar panel voltage1023
solar panel voltage308
solar panel voltage217
solar panel voltage217

led is ON full-time
 

amishlol234

Joined Mar 10, 2022
32
This is not connected to battery, measuring at the panel terminals with just the voltage divider connected? Measured using what meter?

With the solar panel on its own, try these two tests. I just want to confirm panel is working OK.
1. What is measured voltage (with multimeter on V range) across its terminals in good sunlight?
2. What is measured current (with multimeter on A range) across its terminals in good sunlight?

i made my tests without voltage divider hooking up solar panel directly with mu multimeter.

1)measured voltage if kept in the direction of direct sunlight it reads around 5.6v.
2) measured current will be aroung 23-24mA under sunlight (didn't check under direct sunlight).
 
Top