All the pins on the Arduino, analog and digital pins can be used as outputs - all you need to do is use the "pinMode()" routine, usually in setup(), to configure the pin - for example to make digital pin 6 an output:
pinMode(D6, OUTPUT);Sometimes it's useful to draw a picture of circuits, we call these "circuit diagrams" or "schematics" - here's a simple one:
Here the wiggly line "R1" represents a resistor we write it's value "470" next to it. A 470 ohm resistor has a yellow-purple-brown (or yellow-purple-black-black) colour code.
An LED is a semiconductor light, they come in lots of different colours - we use this symbol for an LED. It is made from the symbol of a diode with some light coming out of it. Like diodes LEDs only work if they are in the correct way around - if you look closely at an LED you'll see that one lead on the LED is longer than the other - the longer lead is the one that comes out of the flat side of the triangle in the symbol, the shorter one comes out of the point at the other end.
Other symbols represent connections to ground ("GND", 0 volts) or a voltage like 3.3 volts ("+3.3", or "3V3")
In fact the circuit above is the circuit we built when we were introducing you to the prototyping boards:
Your Arduino's output pins can only drive a small current without being damaged - that means you can drive a small, low current, LED through a current limiting resistor - instead of connecting your resistor to 3.3v hook it to an output pin like pin D6 you can turn the LED off or on by turning the output off and on.
In fact you've already done this, the circuit for the blue LED on the D1 is almost the same, it's attached internally to pin D4 in just the same way (see the note below). Open up the blink sketch and change the initialisation of the "led" variable to "D6" instead of "LED_BUILTIN".:
int led = D6; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
Load it up and try it out, you external LED should blink. Look at the program closely "pinMode(led, OUTPUT);" makes pin D6 an output during initialisation. "digitalWrite(led, HIGH);" makes pin D6 drive +3.3v to its output, and the LED goes on, "digitalWrite(led, LOW);" makes pin D6 drive 0v (GND) and the LED goes out.
What do you think this program does? try it out:
int led1 = LED_BUILTIN; int led2 = D6; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); } // the loop routine runs over and over again forever: void loop() { digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(led2, LOW); delay(1000); // wait for a second digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW digitalWrite(led2, HIGH); delay(1000); // wait for a second }
On the D1 LED_BUILTIN is a bit backwards it goes on when you set it LOW and off when you set it HIGH. Try changing your program to do this:
void loop() { digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(led2, HIGH); delay(1000); // wait for a second digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW digitalWrite(led2, LOW); delay(1000); // wait for a second }
Digital outputs can be off or on, but not half way on - so how can we dim an LED to half brightness?
Your arduino's digital outputs can be turned on and off very fast to do what's called "pulse width modulation", if you do it fast enough your eye won't see an LED flicker, if the pin is on more than it is off you LED will be brighter, if the pin is off more than it is on the LED will be dimmer.
Arduinos have a routine you can call:
analogWrite(led, brightness);
"brightness" can be any value from 0 to 255 - 0 is off, 255 is always on, values in between will give varying degrees of brightness
Note: on the D1 the high voltage is 3.3V rather than 5V)
The following program shows how you animate the brightness of an LED:
/* Fade This example shows how to fade an LED on pin 6 using the analogWrite() function. This example code is in the public domain. */ int led = D6; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin D6 to be an output: pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin D6: analogWrite(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); }
This section describes how to do things using components that are not included in the kit
When Arduino pins are used as outputs they can only drive a small amount of current, it's why we use a 1K resistor when we drive LEDs directly, if we try and drive too much current the Arduino CPU will either not provide enough, or could be damaged. The same goes for voltage, if you want to drive something that needs a higher voltage you can't connect it directly to your Arduino, you will damage it.
If we want to use devices that use more power we can use a transistor like a TIP122 (you can find them at Jaycar down town) to drive other devices. Here are some examples:
You can find a TIP122 datasheet here this will help you figure out which pin is which.
In each example the "base" terminal (pin 1) of the transistor is driven from a D1 output pin (on the left) through a 470 ohm or 1K ohm resistor - transistors like this one are current driven, a small amount of current between the "base" and "emitter" terminals (pin 1 and pin 3) causes a large current to flow between the "collector" terminal (pin 2) and the emitter.
In all the examples above change the "+12v" voltage to match the voltage you need, this circuit will work for anything up to about 50V. Any more than that can be dangerous and you should ask an adult before using higher voltages.
The diagram on the left shows how to drive multiple LEDs that need a higher voltage, or a single big bright LED that needs a lot of current - what value should you use for the resistor R2? Here's an article that can help you decide.
A DC motor pulls way too much current for an Arduino to drive directly, the middle diagram shows how you can drive one - notice the 1N4004 diode across the motor, when you switch a motor off it can generate a voltage that can damage the transistor - the diode protects it - you can also find 1N4004 diodes at Jaycar.
The diagram on the right shows how you can drive a relay from an Arduino - a relay is a switch that is turned on and off by an electromagnet, again because the relay has a coil you need a 1N4004 protection diode. You use a relay because you want to switch a high current or voltage circuit, or because you want to isolate your Arduino from another circuit. You should not be designing circuits that use more than about 50v (like mains voltages), they can be very dangerous.
(if you want to use a motor to go forwards and backwards, you may instead want to use a motor controller)
You can buy multicoloured LEDs individually or as strips:
You can drive short ones directly from your D1 and make every LED a different colour. Ada calls the "NeoPixels" and has an excellent guide on how to use them.
Here's a great tutorial showing how to use them with a D1, don't forget the 470 ohm resistor or the capacitor.