Tuesday, May 24, 2011

Arduino UNO: Potentiometer used as a 10-way LED switch


Using similar principles as before, I have used a potentiometer (variable resistor) as an Analog input. I have mapped the value received from the potentiometer (0 to 1023) and converted the value to a number between 4 and 13, which represent the OUTPUT pins that light up the LEDs.

If you would like to see the sketch/code, let me know in the comments.

Monday, May 23, 2011

Arduino UNO: Flex Sensor and LEDs

Using the same LED matrix as before, and swapping the Photo Resistor for a Flex (or Bend) sensor, and a slight modification to the code, I can now have a light show that can be controlled by "Bending".

I used my finger to bend the sensor, but I could have attached it to a plant, a tree or anything else that bends. The bending changes the resistance, and therefore the INPUT value at analog pin 0.

The parts required:
  • Arduino UNO
  • 10 x Red LEDs
  • 9 x 330 Ohm resistors for the LEDs
  • 1 x 10K Ohm resistors for the flex sensor.
  • 1 x Flex sensor
  • Wires and Breadboard to connect it all together
Most of the components needed for this project can be sourced from the Sparkfun Inventor's Kit

The video:


The Sketch:







































The Arduino Code: 

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Flex Sensor and LEDs created by ScottC on 23rd May 2011
updated on 16/05/2012.

-----------------------------------------------------*/

//Flex Sensor Pin (flexPin)
//the analog pin the Flex Sensor is connected to
int flexPin = 0;

void setup() {
for (int i=4; i<14; i++){
pinMode(i, OUTPUT); //sets the led pins 4 to 13 to output
}
}

void loop(){
//Ensure to turn off ALL LEDs before continuing
for (int i=4; i<14; i++){
digitalWrite(i, LOW);
}

/* Read the flex Level
Adjust the value 130 to 275 to span 4 to 13
The values 130 and 275 may need to be widened to suit
the minimum and maximum flex levels being read by the
Analog pin */
int flexReading = map(analogRead(flexPin), 130, 275, 4, 13);

// Make sure the value does not go beyond 4 or 13
int LEDnum = constrain(flexReading, 4, 13);

/*Call the blink function: this will turn the LED on for 10 milliseconds, and keep it
off for only 1 millisecond. You can change the blink rate by changing these values,
however, I want a quick response time when the flex sensor bends, hence the small
values. LEDnum determines which LED gets turned on.*/
blink(LEDnum, 10,1);
}

// The blink function - used to turn the LEDs on and off
void blink(int LEDPin, int onTime, int offTime){
// Turn the LED on
digitalWrite(LEDPin, HIGH);

// Delay so that you can see the LED go On.
delay(onTime);

// Turn the LED Off
digitalWrite(LEDPin, LOW);

// Increase this Delay if you want to see an actual blinking effect.
delay(offTime);
}


The flex sensor pins/legs are a bit fragile, so be careful when poking it into the breadboard.


For more projects - have a look at the ArduinoBasics Project Page

Photo Resistor and LEDs

The SuperBlink sketch allowed us to create a number of LED animations/transitions. But it is only a matter of time before you opt for something more interactive. In this tutorial, we will make use of a photo resistor (or light dependent resistor : LDR) to create an exciting LED display.

Photo resistors are variable resistors which change resistance depending on the amount of light hitting the sensor. When you move your hand closer to the sensor, you tend to block an increasing amount of light, which increases the resistance of the photo resistor. As you move your hand away, the amount of light hitting the surface of the photo resistor increases, thus decreasing the resistance.

The change in the resistance of the LDR, will affect the voltage being read at one of the Arduino's Analog Input pins (A0). As resistance increases, the voltage drops (and vice versa).

V = IR

V = Voltage, I = Current, R = Resistance
The voltage reading will be used to select which LED to turn on


The Video





We now have the power to control which LED we want to light up. This would look very nice with different coloured LEDs, but unfortunately I am stuck with the Yellow and Red ones from the Sparkfun Inventor's Kit.

Fritzing Sketch


    Image created using Fritzing : http://fritzing.com



Arduino Code


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* ========================================================
    Project : Photo resistor (Light dependent resistor) and LEDs
     Author : ScottC
    Created : 23rd May 2011
Description : Use a photo resistor to create an LED animation.
============================================================
*/

//Define the analog pin the photo resistor is connected to (A0)
int photoRPin = 0;

void setup() {
    //initialise the LED Pins as OUTPUTS
    for (int i=4; i<14; i++){
      pinMode (i, OUTPUT);
    }
}

void loop(){
    //Turn off all the LEDs before continuing
    for (int i=4; i<14; i++){
      digitalWrite(i, LOW);
    }
    
 /* Read the light level:
     Adjust the analog reading values ranging from 120 to 600
     to span a range of 4 to 13. The analog reading of 120
     is when there is maximum resistance, and the value of 600
     is when there is minimum resistance. These analog readings
     may vary from photo resistor to photo resistor, and therefor
     you may need to adjust these values to suit your particular
     LDR. */
    int photoRead = map(analogRead(photoRPin), 120, 600, 4, 13);
    
    /* Make sure the value of photoRead does not go beyond the values
      of 4 and 13 */
    int ledPin = constrain(photoRead, 4, 13);
    
    /* Turn the LED on for a fraction of a second */
    digitalWrite(ledPin, HIGH);
    delay(10);
    digitalWrite(ledPin, LOW);
}
    



If you liked this tutorial - please support me here:




Sunday, May 22, 2011

SuperBlink

Now that I have had a chance to play around with various INPUTS and OUTPUTS, I think I need to show you some of my own creations. This is what is so good about Arduino. You can easily build from the experience of others, and introduce your own creative flair.

When I got my Arduino out of it's packaging, I did not know how fragile it was, I was so scared that I was going to break it. You'll soon find out that it is not as fragile as it looks, but I tend to treat it like an Egg anyway.

Getting Started:
Before you even plug your Arduino into your computer, you are going to need some software to program the little gizmo. Luckily the software is FREE ! Get it here.

Actually, I would recommend that you read the "Getting Started Guide" at the Arduino Web site: and follow the links for your operating system. I use Windows 7 - so this is the guide that I used - here.
I am not going to repeat all of that information, that would be a waste of time.

If you have read the guide, you would have played with the famous "Blink" program. Now its time to supercharge that sucker.


Blink THIS !!

Here is an Arduino sketch that requires the following components.
  • 10 x LEDs (5 Red, 5 Yellow)
  • 9   x 330 ohm resistors
  • 10 x wires to connect it all together.
The Sketch



The video:


The Arduino Code: (Updated Aug 2014)


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

/* ========================================================
       Project : SuperBlink
        Author : ScottC
       Created : 21st May 2011
   Description : A super-charged Blink program
============================================================
*/

void setup(){
  //Initialise the LEDPins to OUTPUT
  for(int i=4; i<14; i++){
    pinMode(i, OUTPUT);
  }
}

void loop(){
  redYellow(3);  //3 replicates of the redYellow sequence
  curtain(2);    //2 replicates of the curtain sequence
  fastSlow(2);   //2 replicates of the fastSlow sequence
}

void redYellow(int Reps){
  for(int k=1; k<(Reps+1); k++){
    for(int i=4; i<14; i=i+2){
      blink(i,200,20); //Call the blink method below
    }
    for(int i=13; i>3; i=i-2){
      blink(i,200,10); //Call the blink method below
    }
  }
}

void curtain(int Reps){
  for(int k=1; k<(Reps+1); k++){
    for(int j=1; j<257; j=j+j){
      int LEDCounter=1;
      int LEDSwitch=1;
      for(int ledPin=4; ledPin>3 && ledPin<14; ledPin=ledPin+LEDCounter){
        if(LEDSwitch){
          digitalWrite(ledPin, HIGH);
          delay(j);
        }else{
          digitalWrite(ledPin, LOW);
          delay(j);
        }
        if(ledPin>12){
          LEDCounter*=-1;
          LEDSwitch=0;
          digitalWrite(13, LOW);
          delay(j);
        }
      }
    }
  }
}

void fastSlow(int Reps){
  for(int k=1; k<(Reps+1); k++){
    for(int j=1; j<257; j=j+j){
      int LEDCounter=1;
      for(int i=5; i>3 && i<14; i=i+LEDCounter){
        blink(i, j, 10); //Call the blink method below
        if(i>12){
          LEDCounter*=-1;
        }
      }
    }
  }
}


void blink(int LEDPin, int LEDOnTime, int LEDOffTime){
  digitalWrite(LEDPin, HIGH);
  delay(LEDOnTime);
  digitalWrite(LEDPin, LOW);
  delay(LEDOffTime);
}

If you like this post please support me here:

Friday, May 20, 2011

Shopping List

If you have just heard about Arduino and are thinking about getting one, don't just buy the board on it's own. You don't buy a PlayStation / XBox without buying a game, and the same goes for the Arduino. If you just buy the Arduino by itself, you will get bored pretty quickly. In fact, I don't know why the Arduino doesn't automatically come with a breadboard, some wires and a few LEDs. Having said that, there are a few companies out there that have put together some pretty good kits.

I am not going to list them, because I am sure you know how to Google, and I am not going to tell you which one to buy, however I will tell you that I decided to buy the Sparkfun Inventor's Kit. You can work out which one suits you.

The Sparkfun inventor's kit not only comes with an Arduino UNO, but also a wide array of bits and pieces to muck around with. You can buy it in Australia from Australian Robotics.

•Arduino Uno USB
•Arduino and breadboard holder
•Printed 36-page Oomlout manual
•12 color circuit overlays
•Clear Bread Board
•74HC595 Shift Register
•2N2222 Transistors
•1N4148 Diodes
•DC Motor with wires
•Small Servo
•5V Relay
•TMP36 Temp Sensor
•Flex sensor
•Softpot
•6' USB Cable
•Jumper Wires
•Photocell
•Tri-color LED
•Red and Green LEDs
•10K Trimpot
•Piezo Buzzer
•Big 12mm Buttons
•330 and 10K Resistors
•Male Headers

The manual that comes with the kit is not the best manual in the world, but it does redirect you to a much more useful site online. I thoroughly recommend this kit for beginners. It will point you in the right direction, and once you start tinkering, there is no turning back. You won't stop talking about it. Totally awesome.

And when you get through the 12 sketches (and their recommended variations), you can't help but feel a sense of power. The power to create light, create sound, create movement, and the ability to discover the world around you like you never have before.

Regardless if you decide to buy a kit or not, there are a few individual items that are essential. If you don't buy them now, you will later on.
  • Multimeter
  • Soldering iron
  • Helping hands with magnifyer - good for holding that component / wire when you need a 3rd hand
  • Alligator clips
  • Breadboard
  • Wires - to connect your sensors/motors etc  (Male/Male wires) and (Female/Female wires)
  • LEDs
  • A button or a switch of some sort - preferably one that can be put on a breadboard.
  • Resistors (330K and 10K)
  • External Power supply for Arduino board :  to liberate your Arduino

Feel free to put a recommended kit in the comments.

Tuesday, May 17, 2011

From Blank to Blink

I have to start somewhere, so why not start NOW.
This blog aims to capture my journey of discovery as I dive head first into the world of electronics, armed with an Arduino in one hand and a borrowed multimeter in the other.

I aim to share my learnings with you, and not only highlight my successes, but also my failures.
I have a curious mind, so I hope I don't set anything on fire.
If you decide to follow in my footsteps, you do so at YOUR own risk.

I would prefer if you used me as an example of what NOT to do, but if I inspire you, please let me know.


Image made with Fritzing : http://fritzing.org