Read solar panels a button?

Hi everyone!

I'm trying to use my Arduino to keep track of how many times a relay turns a light on and off. The simplest way I can think to accomplish this would be to read input from a solar panel set up in front of the light bulb. To test the concept I've modified the sample "button" code to turn on the LED when voltage comes in from the solar panel, but it doesn't seem to be working.I have the solar panel positive lead feeding into pin 8, and the negative solar panel lead to ground. Here is the code as I have it so far:

// the setup function runs once when you press reset or power the board
const int led=13;
const int solar=8;

int solarstate = 0;

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(led, OUTPUT);
pinMode(solar, INPUT);
}

// the loop function runs over and over again forever
void loop() {
solarstate = digitalRead(solar);
if (solarstate == HIGH) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}

}

Any thoughts or input would be greatly appreciated!

Thanks!

Hi,

Do you have a multimeter of some sort??

What size / type is the "solar panel" ?? What is it's rated Open Circuit Voltage?

You need to put the panel in series with a resistor so that there is a point that changes voltage with illumination on the panel.

That said, a simple photoresistor sensor and resistor should work fine...

If you don't have a multimeter you can try running the Read Analog Voltage Example. You may not be getting enough voltage to measure a logic-high.

ki6svs:
when voltage comes in from the solar panel,

A Light Dependent Resistor (LDR) or a photo transistor would be much more suitable for this task.

...R

OK, changed gears. I found a photoresistor, and it works as the button in the button tutorial!

Now on to part 2 of the problem, I need to count the cycles and send the count to a serial monitor. The problem I now see is when the light hits it, it goes into rapid count mode and just keeps scrolling. How do I convince the setup to add 1 per light flash?

Updated code:

/ the setup function runs once when you press reset or power the board
const int led=13;
const int photo=8;
int photostate = 0;
int count=0;

void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(photo, INPUT);
}

// the loop function runs over and over again forever
void loop() {
photostate = digitalRead(photo);
if (photostate == HIGH) {
digitalWrite(led, HIGH);
count ++;
Serial.print(count);
} else {
digitalWrite(led, LOW);
}

}

State change detection

You need to increment the count when the state of the LDR changes from LOW to HIGH.

...R

LDR are not Logic they are Analogue.
Hooking one up and reading it as a button will give spurious readings (hence the multi count i believe).

Google "Using an LDR on Arduino" or have a look at the tutorials.

It's giving the high counts because it adds one every time the pin is read HIGH. It should overflow as quickly as 9600 baud printing lets it and I don't see any code setting it back to zero.

Any large enough shift in voltage (while not exceeding VCC) can be used digitally.

In a pinch, a led can be used as a light detector just by placing the short lead in ground and the long one in A0. Expose to bright light and don't read A0 very often (analog read drains a tiny bit of charge) and you can see > 1V. A few quick reads will show the value drop fast. It's slow but it's easy and it works.

Daz1712:
LDR are not Logic they are Analogue.
Hooking one up and reading it as a button will give spurious readings (hence the multi count i believe).

The pin hysteresis will prevent minor LDR fluctuations from changing the binary state.

See LDR response time, that's as fast as it can change which is pretty slow except compared to humans.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom..... :slight_smile:

Daz1712:
LDR are not Logic they are Analogue.

If they are wired with another resistor to make a voltage divider and if there is a significant difference between light levels they can be detected with digitalRead() - much simpler than using analogRead()

...R