To use information from a photo resistor to change the brightness of a LED.
Equipment:
- 1x Arduino Uno
- 1x Breadboard
- 1x LED
- 1x 330 Ohm Resistor
- 1x 10k Ohm Resistor
- 1x Photo Resistor
- Who knows how many wires
Reference:
Program Details:
The new piece of hardware being introduced here is a Photo Resistor. This works like the Potentiometer in the sense that it returns an analog signal, but unlike a Potentiometer it is controlled by environmental factors. The Photo Resistor also only has two pins, for the electricity to pass through. So the information must be takes in after the current has passed through. The amount of voltage the reading pin receives is dependent on how much light is present, with brighter areas receiving low voltages.
The experiment also introduces two new methods on the programming side. These two methods are map(value, fromLow, fromHigh, toLow, toHigh); and constrain(value, low, high); Map takes a value from on range, and then scales it down to another range. This is done in this program because Arduino can only output 8-bit, so the number given by the Photo Resistor needs to be scaled down. We also use constrain because map() does not set limits to how high the value can be, it only scales. So constrain() insures that the value will not exceed the 8-bit limit.
To put this together we first created an independant circuit, to power 1 LED. The power for this LED comes through Pin 13. In the second circuit, we had 5V plass through a Photoresistor. After it has passed through we read the current with Analog Pin 0. Afterwards the electricity continues through a 10K ohm resistor, and is then grounded.
Results:
At first glance the program appeared to be working fine, as the LED was lit and we were in a bright room. After I placed my finger directly on the Photo Resistor, I was expecting the LED to go off. Rather in only got dimmer, which maybe due to the room being too bright or a faulty sensor.
Photo:
Tips:
Make sure you don't forget to place the 10K oh resistor between the analog pin and ground. Else Pin 0 may get a faulty reading.
Further Work:
You can create a threshold for the Photo Resistor, so it can behave like a pushbutton; in the sense that its only two options for the LED would be on and off. To do this create a threshold so that when the lighlevel exceeds the threshold - the LED turns on, and if the lightlevel is lower than this threshold the LED turns off.
Program Modification;
The program is like the one found in the book and in the reference:
Program:
int lightPin = 0; //Analog pin which reads the info from the Photo Resistor
int ledPin = 9; //A PWM digital pin which controls the LED
void setup()
{
pinMode(ledPin, Output); //Sets Pin 9 to only output
}
void loop()
{
int lightLevel = analogRead(lightPin); //Sets light level to the info taken from Pin 0
lightLevel = map(lightLevel, 0, 900, 0, 255); //Changes the range from 0-900 --> 0-255
lightLevel = constrain(lightLevel, 0, 255); //Value cannot exceed this range, and is capped
analogWrite(ledPin, lightLevel); //Outputs the value (brightness of LED)
}
No comments:
Post a Comment