User Tools

Site Tools


External Components

This stage involves the following new components.

More Blinkenlights!

One LED was fun, but how about five? Replicate the LED/resistor circuits on your breadboard 4 times, using a different pin on the Arduino for each LED.

challenge_yourself.jpg Easier
(1) By modifying the code from the single LED program, and using the knowledge you've learn from the presentation, make the LEDs flash in the sequence 1,2,3,4,5,1,2,3…

(2) Now change it so that the LEDs flash in the sequence 1,2,3,4,5,4,3,2,1,2…

Controlling direction

We're now going to use the push button to control the direction of the LEDs going through their sequence. When the button is not pressed the lights should go 0,1,2,3,4,0,1,2,3…, when it is pressed the lights should go 4,3,2,1,0,4,3,2…

Note: the buttons in your kit have bent legs. It's worth using a pair of pliers to straighten them out.

  • Put one of the switches into the breadboard and wire to pin 7 as shown in this photo.

Below is the code for controlling LEDs with the push button. It stops the LEDs when the button is pressed.

// Button is on pin 7
int button = 7;

int time = 100;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pins as outputs.
  for(int led = 9; led < 14; led++) {
    pinMode(led, OUTPUT);
  // set the button pin as an input
  pinMode(button, INPUT);
  // enable the internal pull up resistor
  digitalWrite(button, HIGH);

// the loop routine runs over and over again forever:
void loop() {
  int buttonReading = digitalRead(button);
  if(buttonReading == 1) { // If the button is not pressed
    // Do a sequence
    for(int led = 9; led < 14; led = led + 1) {
      digitalWrite(led, HIGH);
      digitalWrite(led, LOW);

    for(int led = 12; led > 9; led = led - 1) {
      digitalWrite(led, HIGH);
      digitalWrite(led, LOW);
challenge_yourself.jpg Harder
(1) Try to change the code so that the LEDs change direction while the button is pressed.

Controlling Speed

We are now going to try to use the rotation sensor, otherwise known as a potentiometer or variable resistor, to control the speed of the LEDs. First, have a look at this video which explains the theory of voltage dividers.

Our rotation sensor is simple a voltage divider circuit in which the resistance of the two resistors change when you turn the knob. This means the voltage between the two resistors changes when you turn the knob. We are going to use the Arduino's analog inputs to measure this voltage accurately, then use the measurement to control the length of the delay we have in our loop, and hence the speed of the LED flashing sequence.

The Arduino's analog inputs are on the left side of the board and are labelled A0 through to A5. You can read the voltage on pin A0 as follows:

// The reading will be a number between 0 and 1023, where 0 is 0V, 1023 is 5V, and all the numbers in between are the voltages inbetween
// For example a reading of 512 (which is right in the middle of the range) would be a voltage of 2.5V.
int reading = analogRead(0);

Place the rotation sensor on the breadboard and wire it up to the Arduino as follows:

Remember using the serial communication in Lesson 1 to allow the Arduino to send data back to your laptop? We can use that to see how the value output by our rotation sensor varies as we rotate it.

void setup() {                
  // initialize serial communication at a speed of 9600 bits per second:

void loop() {
  int reading = analogRead(0);
  Serial.print("Analog reading: ");
  delay(500);               // wait for half second
challenge_yourself.jpg Easier
(1) Use the reading of the rotation sensor voltage, change the length of the delay in the loop. This should cause the LED flashing sequence to change speed when you turn the knob.

Next Stage

Congratulations. Now you can go onto Stage 3!

workshops/arduino_for_the_curious/stage_2.txt · Last modified: 2017-04-10 11:22 by Simon

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki