First we are going to look at flashing an LED, this is the 'hello world' of electronics.
- Open the Arduino environment
- Go to the 'File' menu and open the 'Examples => 01.Basics => Blink' sketch.
- Plug the Arduino board USB cable into your computer and hit the Upload button in the software (the arrow in a circle)
- Marvel as the on-board LED flashes!!
Lets do this properly and flash an LED on a breadboard using a button. Go ahead and build the following circuit, paying attention to the flat side of the LED with the short leg, this is the ground side. Some components have polarity, i.e. they care about which way round they are, whereas others do not (like the resistors).
- Now we are going to open the 'File' menu and open the 'Examples => 02.Digital => Button' sketch.
- The sketch has a description and a link to a tutorial at the top, follow it here.
- Have a look at the tutorial, run the code and test out your button
- Try to contain your excitement
Just take a moment to read through the code here, it is well annotated and tells you exactly what each line is doing. It is separated into three parts, the top where the definitions occur, the 'void setup' section which runs once at the beginning of the code, and the 'void loop' which is the main loop and runs repeatedly VERY QUICKLY.
const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin
It is good practice to use names to refer to the pin numbers within the code and declare that association at the top of the code, so if you later want to change the pin you don't have to go through the whole code and change every reference, you can just change it at the top. In this case the button pin and LED pin are declared as 'const int's i.e. constant integers, or unchanging whole numbers in English.
Imagine the variable in this case as a container that you can keep a number in. You can change the number inside and check what is in it at any time by calling its name when you want to within the main code.int buttonState = 0; // variable for reading the pushbutton status
The comments say it all really...void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT);
}
void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }So, we store the reading from the button pin in our 'buttonState' variable that we created at the beginning, and then we can check it and turn on the LED with an 'if' statement (that we will explore more later).
So, we have just made a digital input (a button that has two possible states of on or off) control a digital output (an LED that can be on or off). But what if we want to fade the LED nicely? Have a look at the next tutorial to find out.
Workshop 1- Hello World!
Simple use of a button (a digital input) to turn an LED on
Workshop 2 - Analog inputs/outputs
Reading a fader/knob/sensor (analog input) to fade an LED
Workshop 3 - PWM and servo motors
Control a servo motor using Pulse Width Modulation
Workshop 4 - Fritzing!
Having a look at circuit design using Fritzing
Workshop 5 - Soldering
Some tips on soldering
Workshop 6 - Serial Communication
Exploring how to communicate with the computer via serial
Workshop 7 - Controlling Max MSP
A look at Max MSP and how we can speak to it from Arduino
Workshop 8 - Making a video system
A look at some of the built in video modules of Max and using Arduino to control them
Workshop 9 - Max to Arduino
Going the other way and using a Max user interface to control Arduino hardware