Photoresistor controlling LED

With a photo resistor you can control something from your Arduino board depending on light intake. The following circuit shows how to connect your photo resistor to the Arduino board and read the values.

  1. int ledPin = 3;
  2. int photocellInput = 0;
  3.  
  4. void setup()  {
  5.   pinMode(ledPin, OUTPUT);
  6. }
  7.  
  8.  
  9. void loop()  {
  10.  
  11.   photocellInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
  12.  
  13.   analogWrite(ledPin, photocellInput);  
  14.   // The delay can be change to get the desired dimming effect
  15.   delay(20);                            
  16. }

Decision maker with LEDs

The following circuit and code shows an example of a decision maker made with LEDs. Pushing the button will make the LEDs flash in a random order for 1 second. Then displaying the random decision where one LED lights up for 3 seconds and then reseting.

  1. int timeShowRandom = 1000;
  2. int timeShowDecision = 3000;
  3. int timeBlink = 50;
  4. int buttonPin = 3;
  5.  
  6. int buttonPress = false;
  7. int randomNumber;
  8. int previousNo = 0;
  9. int timePassed = 0;
  10.  
  11. void setup() {    
  12.   // Set button pin
  13.   pinMode(buttonPin, INPUT);
  14.   // Set output pins  
  15.   pinMode(12, OUTPUT);
  16.   pinMode(11, OUTPUT);
  17.   pinMode(10, OUTPUT);
  18.  
  19. }
  20.  
  21. void getRandomNo() {
  22.   int rand = random(10,13);
  23.   if(rand == previousNo) {
  24.     getRandomNo();
  25.   } else {
  26.     randomNumber = rand;
  27.     previousNo = randomNumber;
  28.   }
  29. }
  30.  
  31. void loop() {
  32.   // Check if button is pressed
  33.   if(digitalRead(buttonPin) == HIGH && buttonPress == false) {
  34.      buttonPress = true;
  35.   } if(buttonPress == true && timePassed <= timeShowRandom) {
  36.     getRandomNo(); // Get random pin number
  37.     digitalWrite(randomNumber, HIGH);
  38.     delay(timeBlink);  
  39.     digitalWrite(randomNumber, LOW);
  40.     delay(timeBlink);  
  41.     timePassed = timePassed + (timeBlink * 2);
  42.   } else if(buttonPress == true) {
  43.     digitalWrite(random(10,13), HIGH); // Set random pin on
  44.     delay(timeShowDecision); // For x seconds
  45.     buttonPress = false; // Set button to be enabled again
  46.     timePassed = 0;
  47.   } else {
  48.     // Reset all output pins
  49.     digitalWrite(10, LOW);
  50.     digitalWrite(11, LOW);
  51.     digitalWrite(12, LOW);
  52.   }    
  53. }

Using a potentiometer to control LED

You can easly control the current running through your LED by adding a potentiometer as part of your circuit.

To control the LED with Arduino programming you attach the potentiometer to your analog in and let your Arduino program decide how much to dim the LED depending on the input you get from the potentiometer.

The input from analogRead returns a value between 0 and 1023. The analogWrite takes values between 0 and 255. The code below show you have to convert your analog in value to make your LED shine as bright as possible when the potentiometer is fully on.

  1. int ledPin = 3;
  2. int potentiomenterInput = 0;
  3.  
  4. void setup()  {
  5.   pinMode(ledPin, OUTPUT);
  6. }
  7.  
  8.  
  9. void loop()  {
  10.  
  11.   potentiomenterInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
  12.  
  13.   analogWrite(ledPin, potentiomenterInput);  
  14.   // The delay can be change to get the desired dimming effect
  15.   delay(20);                            
  16. }