Nov 062011
 

Using a Potentiometer with Arduino: 5 LED chaser:

In this activity we connect a potentiometer with the Arduino, and use it to control the speed of 5 flashing LEDs.

arduino-5led-pot-fr

To build this activity you’ll need:

  • Potentiometer
  • 330 or 470 Ohm resistor
  • 5 LEDs
  • jumper wires
  • breadboard
  • Arduino

 

arduino-5-leds-potentiometer01

1) Connect 5 led’s to the breadboard so that the positive lead of each LED can be easily connected to pins 8,9,10,11 and 12 of the Arduino. The negative leads should be connected to a 330 Ohm (minimum) resistor which leads back to ground.

arduino-5-leds-potentiometer02

2) The potentiometer will have a positive, negative, and wiper connection. The wiper should go to pin 0 of the analog inputs while positive and negative go to +5 and Gnd repectively.

Arduino potentiometer & 5 LED chaser code:

 

int sensorValue = 0;    //make a variable where you can store incoming
                        //analog values

void setup(){
  pinMode(12, OUTPUT);  //tell arduino what you'll be using these pins
  pinMode(11, OUTPUT);  // for (output).
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);

  Serial.begin(9600);  //initialize serial
}

void loop(){      //we put the code we want executed in a loop 

Serial.print("sensor = " );  //sends what's in quotes via serial
Serial.println(sensorValue); //sends our variable (sensorValue)
                             //via serial   

  digitalWrite(12,HIGH);       // lights the led
  sensorValue = analogRead(0); // reads pin 0
  delay(sensorValue + 25);     // sensorValue used for delay
  digitalWrite(12,LOW);        //turns off the led
  delay(15);                   //delay before moving to next output pin
                               //the + 25 keeps delay from reaching zero
           //code below is for remaining 4 LEDs
  digitalWrite(11,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(11,LOW);
delay(15);

  digitalWrite(10,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(10,LOW);
delay(15);

  digitalWrite(9,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(9,LOW);
delay(15);

  digitalWrite(8, HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(8, LOW);
delay(15);

  digitalWrite(9,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(9,LOW);
delay(15);

  digitalWrite(10,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(10,LOW);
delay(15);

  digitalWrite(11,HIGH);
  sensorValue = analogRead(0);
  delay(sensorValue + 25);
  digitalWrite(11,LOW);
delay(15);

}

How it works:

As we turn the potentiometer knob we vary the voltage reaching analog pin 0. This voltage (0-5volts) is converted to a value between 0 and 1023. This numerical value is used to change the delay between blinks in the code above, controlling the speed at which the light “moves” from LED to LED. Since we wanted a minimum delay of 25 (any faster didn’t look right) we simply added 25 to the delay value in the code (sensorValue +25).

 

Feb 122011
 

One of the first and simplest sketches to run on your Arduino is to make an LED blink. If this is your first time using Arduino, read through our “Arduino basics: getting started” article.

Connecting the Arduino and LED

Connect your Arduino to your computer using a USB cable. The power light should come on.

1) Place your LED and a 470 ohm resistor in series (a breadboard will help). Always use resistors with LEDs for safety purposes, and remember that the LED cathode points toward Ground.

2) Connect jumper wires to pins 13 and Gnd on the digital side of the Arduino

3) Open the arduino IDE and type in the following code:

void setup() {
    pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

4) Press the upload button in the IDE to send the code to your Arduino. It should automatically start running it!

Video of the LED blinker in action

(includes code explanation):

.

Modify the LED blink sketch:

  • You can change the blink rate by changing the delay!
  • You may have also noticed a little yellow LED on the board that blinks in time with your LED. That little LED is wired to pin 13 and is used for testing purposes. If you want to avoid it, use a different pin (such as pin 12). But remember to modify the code (see below)
void setup() {
    pinMode(12, OUTPUT);
}
void loop() {
  digitalWrite(12, HIGH);
  delay(500);
  digitalWrite(12, LOW);
  delay(500);
}

The above code is modified to use pin 12 (instead of pin 13) and to only delay for 500 ms. Try your own variations!

Jan 302011
 

This project requires you to be somewhat comfortable with the breadboard and placing components in the proper sequence.

In particular, pay attention to:

  • The negative stripe on the capacitors (should always point to ground)
  • The pins of the transistors (its helpful to know which are E,B, and C)

To build this circuit:

  • 9V battery or power source
  • Transistors (two NPN, we used 2n3904)
  • Capacitors (we used 100uF)
  • LEDs (2)
  • resistors (2 of 10k Ohms)
  • resistors (2 of 470 Ohms)
  • push button switch
  • jumper wires

Alternative breadboard layout for LED flasher:

If you want a simpler layout with less jumper wires, here’s another version we built. It’s the same circuit, but the transistors and capacitors mirror each other so be mindful of polarity!

alternate layout

Video of dual LED flasher in action:

where I test the circuit in Ktechlab before showing the build test

Jan 192011
 

Starting out with a beginner’s LED circuit:

simple led circuit, light an led

A schematic of a simple led circuit

Remember DO NOT GO HIGHER THAN 9 VOLTS for this circuit (whether using a battery or power supply)!

RESISTOR (470 ohms) – this drops the 9 volts from your source down to a good level for your LED.
LED – this is a diode that only lets current flow in one direction, but it also gives off light  (Warning! Putting more than 3v through a LED may kill it. They can shatter, so use a resistor in series with it!)

Simple LED circuit using a breadboard

Simple LED circuit using a "breadboard".

The LED is a “polar” component – electrons enter a LED through the cathode leg of the LED. If the LED doesn’t light, it may be in backwards. Make sure the flat side of the LED is facing your “negative wire” or ground.

LED circuit Questions:

  1. Use a picture editor to draw a red line on the graphic showing the path of the electricity (Think: Electrons begin moving from the negative pole of your supply, through the circuit components, and back to the positive pole  (this may seem opposite to conventional current).
  2. measure and record the voltage of your power source
  3. measure and record the voltage drop across the resistor
  4. measure and record the voltage drop across the LED
  5. add up the voltage for of everything except your source
  6. what is the relationship between the source voltage and the total of your voltage drops?