Avoid shorts and keep your battery pack chill.
Category: child-page
a way to quickly manage side bar toc’s
Electronics Primer
Introduction to Ohm’s Law and the basic characteristics of electricity.
Video :: Terminology
Get the Slides
RGB LEDs – So Much Color!
RGB LEDs are fun components to explore expressive and technical aspects of tangible media. You can control them in circuits with fixed resistors, or with code as digital or analog devices. The possibilities and combinations are endless.
RGB LEDs are also examples of parallel components — the three color channels are organized internally in parallel. This arrangement makes setup a little more difficult than simple LEDs but the potential color variations make it worth the effort.
Required Parts
- breadboard
- battery pack
- battery clip (wire)
- resistors assorted (start with 1kΩ)
- resistor 470Ω
- RGB LEDs (big white gumdrop)
Power connections
In this video I use a voltage regulator that we no longer have in our kits (hmm) . For a raw circuit (no code), you have two solutions possible with the current kit:
First, use the 9V battery pack we have been using in class and connect to the breadboard power rails. The resistors suggested will protect the LED at 9V
Second — and we may not have covered this in class — but you can use your Arduino as a power source. Connect the Arduino pin labelled +5V to the red breadboard rail and GND connected to the blue breadboard rail. This will provide a stable 5V supply.
Use dupont wires or cut your own to make these connections — double check for short circuits before you power up.
Arduino Connections for power
Video
Get The Slides
The Circuit
The RGB LED has three single color LEDs inside – red, green and blue. Each of these behaves in essentially the same way as a single color LED on its own. There are slight difference in the amount of voltage it takes to turn on each color in the RGB LED.
Finding Ground
Take a moment to find the ground leg of your RGB LEDs. The ground (GND) leg is the longest. There is no flat side on the case of these LEDs.
Hold your RGB led such that there is one shorter leg to the left of ground, and two shorter legs to the right of ground. If held like this, the legs go in order from left-to-right : RED – GROUND (GND)-GREEN-BLUE. In the symbol above, the leg below the LEDs is a common ground connection for all three internal LEDs. Keep track of the ground leg as you build.
Single resistor circuit.
Use this single resistor circuit to test each channel one at a time.
Try moving the top resistor ( 1k in schematic — connected between Arduino 5V [+ve] and the BLUE channel) to the RED and GREEN LED legs (excluding ground). You should be able to confirm the leg order described above and the channel colors.
Mixing Colors
Using the fixed resistors in your kit, try creating your favourite color. Try different values for the top 3 resistors in the schematic below. You can use any resistor from your kit AS LONG AS YOU ALSO LEAVE the 470 ohm resistor shown in place.
What is your favourite color?
Going Further
LED + Resistor = Brightness
After building a few circuits you should start to see that LED brightness can be controlled by altering the size (resistance) of the current limiting resistor used in the circuit.
LED Brightness and Current Flow
We know from Ohm’s law (V=IR) that for a fixed voltage (5v in case of an Arduino digital pin) current is inversely proportional to resistance.
What does that mean?
If we hold voltage constant:
– increases in resistance will reduce current.
– decreases in resistance will increase current.
LED brightness is proportional to current — so small resistors give bright LEDs and big resistors give dim LEDs.
Note if resistance gets too small, you will have dead LEDs not bright ones — see why here.
Let’s build some circuits to explore this relationship.
Required Parts
- breadboard
- battery pack
- wire battery clip
- leds (single color all the same)
- resistors — fixed several
- Ohm’s law chart
Video
UPDATE FOR 2023:
In the following video I use a battery pack to provide power (we will look at batteries soon). Remember circuits have two complimentary halves: the source side and the sink side. You can work through this video with an Arduino for power instead of battery power. The build will follow a parallel path after you have your power source setup.
One big difference is that a battery pack provides 9V — instead of 5V. You can imagine this as a tall vs short waterfall (charge difference). So in the video i talk about 9V — where, if powered from Arduino, you will only have 5V. The principles discussed remain the same.
GETTING SET UP: To use your Arduino for power, set up the power side of your circuit like the image below — the action on the breadboard does not change. At this stage dupont wires are your friends — use red and black if you have them — or others colors that are available. (Remember you need to connect your Arduino to a plugged in USB cable — not shown — so that the Arduino has power!).
In the image above the red wire is connected to 5V from the Arduino and the top red rail on the breadboard. The black wire is connected to Arduino Ground and the blue rail on the bottom of the breadboard.
With that completed you are ready to watch the video!
Going Further
Current Limiting Resistors for LEDs
I find current to be one of the trickiest concepts to grasp in introductory electronics. Part of this is because current does not behave the same way in all components. Part of it is because current flow is tied to both your power supply, the thing being powered ( fancy term == load) and the total impact of all parts in a circuit.
Current Flows in LEDs
This Building Block explores how current flows through an LED. It also explains why we use current limiting resistors with LED and how to calculate their size.
Recall Ohm’s Law
Resistors follow Ohm’s law
Current flows through a resistor following Ohm’s law (V=IR). For a fixed resistor — say one of our favourites like a 1K Ω (brown, black, red) — Ohm’s law tells us that if we increase voltage across the resistor, we will proportionally increase current.
LEDs are Different
LEDs act differently. They are diodes — Light Emitting Diodes! And diodes are semiconductors. So, they follow slightly different rules (for a deeper dive into this).
We have seen that our LEDs block current and do not light up when placed in a circuit ‘backwards’ (fancy term == reverse biased). We have also seen that they glow delightfully when placed in circuits with their flat sides to ground (fancy term == forward biased).
Characteristic Forward Voltage
The amount of voltage required to turn an LED on, or make it glow, is called the characteristic forward voltage (VF). The exact value is dependant on the diode — it differs slightly for each color — but is usually 1.4-4V. At the maker / creative level of electronics, that we are exploring, most people assume 2V.
Break Down VOltage
If you exceed the characteristic voltage and give the LED more V than it takes to turn on, then the resistance of the LED drops very fast. When this happens Ohm’s law tells us that LED will use (fancy term == draw) a whole bunch of current. This can, in some cases, lead to LED burn out; along with electric melting smells, LED caps occasionally becoming projectiles and a total lack of glow.
Avoiding LED meltdowns
In addition to a forward voltage spec, LEDs have a characteristic forward current or recommended operating current that should not be exceeded. We can protect our LEDs from the rapid current increases by placing a resistor in series (one after the other) with the LED. We call these resistors current limiting resistors. Note any resistor can be current limiting as long as its placed in series with the LED — its the placement of the component not its brand or type or materiality that makes it current limiting.
Calculating Resistor Size
With this bit of background understood we can jump into determining the size of resistor we need to protect our LEDs.
Let’s assume we are using our basic LED circuit. In this circuit we have a power supply (V), an LED with a set operating (forward) current (I) and a series resistor (R) that limits the current.
We can use Ohm’s law to calculate the size of resistor needed.
In the image above, cover the R and see that resistance (R) is calculated by dividing V / I. Simple. But what V and what I?
We should know V for the circuit — it will be a battery pack of known voltage (9V from out kits) or from Arduino pin (5V for an Uno). If you power up with other sources make sure you know the voltage!
The Power of Data Sheets
Current limits can only be reliably found in the data sheet of the LED you are using. To find this you need to know what LED you have in hand, i.e. what part you bought.
Data sheets are technical documents that outline everything you could want to know about a component. The data sheet for our RED LEDs is mercifully short; only 3 pages. The data sheet for the microcontroller on an Arduino Uno is longer than 300 pages.
The first page of the red LED sheet tells us everything we need to know. It includes the following chart:
Recall that two values are needed for our calculation — forward voltage (turn on voltage – red), and operating current (sometime called forward current- blue).
Forward voltage is listed as being between 1.7 and 2.6V with 2.1V indicated as typical.
Operating current is 10 mA or 0.010A. Max listed as 50mA ( 0.050A). If you exceed the max of 50mA, the LED is a goner.
Formal Calculation
For our basic circuit we can calculate resistor size using the following formula:
where:
R == size of resistor we need
Vs == source voltage ( battery or Arduino for us)
Vf == forward (turn on) voltage of the LED
I == operating current of LED — in AMPS (A). Often this current is listed in mA (milliAmps). If so, you will have to divide mA by 1000 to convert to amps (A).
Battery Powered Resistor
For the battery powered LED and resistor circuit that we started this course with (Alligator Clip Circuit) we would have the following values in the formula:
Which gives a resistance of 690Ω.
(You can confirm with a calculator or back of an envelope).
We do not have a 690Ω resistor in our kit (though they exist — what would their color bands be ? ). Instead, we go to the next size up; which in our case is 1KΩ.
Quick and Dirty Calculation
Given that we are making experiences and creatively applying electronics, not making heart rate monitors, we can take a few short cuts. Engineers close your eyes for a moment.
You can simplify this calculation — and I do this all the time — by ignoring the forward voltage of the LED. If you don’t have a data sheet you could also use 2V and apply rule above.
The resulting simplified formula looks like this:
And for our red LEDs with a recommended current of 10mA (.010A) — we would calculate R = 9/0.010 == 900Ω. The closest, next bigger resistor in our kit is 1KΩ. This is the same result as above!
When you ignore forward voltage you end up with a calculation that OVER ESTIMATES the resistance needed. In other works you will use a bigger than needed resistor. The trade off is a circuit with a slightly less bright LED (usually undetectable) in a circuit with too much protection. I think its a good trade-off when data sheets aren’t handy.
What if we don’t know LED current?
It is common in maker tutorials to see people assume the LED current is 20mA (.020A). This a compromise / guess — and it happens because most kits and parts you buy online will not have readily available links to component data sheets.
If you don’t have a data sheet this is generally a good point of departure for LED current. If you use this and things get hot or you melt an LED, get the next bigger resistor in your kit.
This simplified formula for cases when LED current and LED voltage are both unknown is:
For our 9V circuit we would calculate R= 9V/0.020A == 450Ω.
Closest, next largest resistor in our kit is 470Ω. This is smaller than the 1K predicted above.
Current flow with a red LED would be I = V/R == 9/470 == 0.019mA — which is much less than peak current limit. So, you may shorten its life but it won’t explode.
Arduino Pin Considerations
When we connect any circuit to an Arduino pin we need to also consider the current limits of the Arduino.
Each individual pin can handle an absolute limit of 40mA (0.040) based on data sheet (Arduino site says 20mA). However, the Arduino Uno as a whole — all the current, in all the connected circuits, added up, can not exceed 200mA (0.20A).
As our circuits get more complex, we will need to pay attention!
What resistor do we need if we have a 5V I/O pin and unknown LED current and voltage limits.
In his case we use the simplified formula and assume 20mA through the LED. This is less than the Arduino pin limit and should be LED safe.
R == 5V/0.020 == 250Ω. Bigger than our 100Ω resistor, but less than 470Ω — so use at least a 470Ω kit resistor.
If we account for LED forward voltage and use data sheet current.
R == 5-2.1 / 0.01 == 290Ω.
Again, select at least 470Ω.
Playing Safe, Protect your Arduino = 1KΩ
Because of the Arduino hard limit of 200mA — I generally build simple LED circuits with 1kΩ resistors. This means slightly dimmer LEDs than may be possible, but helps keep total current low.
At 5V and 1000Ω (1k) the current supplied by a pin is <5mA. You could safely put one of these on every i/o pin and not exceed the total device limit!
Going Further
Explore the ideas in these ideas intuitively — in LED + Resistor = Brightness
More about arduino power limits.
GREEN LED from kit — data sheet
Potentiometers or POTs
Potentiometers or pots are a type of variable resistor. Unlike their fixed resistor cousins, the resistance in a pot can be controlled by the user.
POT Basics
The pots in your kit have two very different resistances; 10K and 500k pots. Pull them out so you can get a sense for what these feel like in your hands. Try turning the dials on each one. Feel how far they move relative to a full circle.
What are POTs? What do they want?
Pots are one of the magical creatures of the electronics world. When we place them in a circuit, we connect one outside leg to the +Ve side of our power supply (top of waterfall), and the other outside leg to ground (bottom of waterfall). The middle leg then outputs a voltage between +Ve volts (9v if battery, 5V if Arduino) ) and ground (0V).
Recall that voltage and current are related to resistance according to relationships defined by Ohms law. So, adjusting resistance alters voltage and current flow. The wiring of the pot is such that the voltage changes are passed out the wiper (middle leg).
How is that possible? We will get into more detail after the video.
POT Representations
The symbol for a pot is based on that of a fixed resistor. Its base is a zigzag line with legs on each end. Added to that is an arrow pointing at the zigzag of the fixed resistor symbol.
The arrow represents the wiper (middle leg). It may help to imagine the whole arrow moving along the fixed resistor symbol from one end to the other. As it moves, the amount of resistance on each side of the arrow changes.
Note that the pin labels would not normally be included, but the resistance would.
Let’s set up a circuit and explore.
Required Parts
- POTs
- 1k fixed resister and chart
- LED (single color)
- hook up wire ( red, black and some other color — maybe yellow )
- wire strippers
- battery pack loaded
- battery clip
Video :: POTentiometer Basics
Slides: Grab a copy
The Circuit
Here is the schematic of the circuit built in the video above.
Note the inclusion of the 470ohm resistor after the LED. This resistor limits the current through the LED and ensures its protected.
Pots are a kind of voltage divider!
As mentioned in the video and the slides, a pot is an example of a voltage divider. Voltage dividers are very important kind of circuit — they are used with many sensors.
I will have to create a voltage divider tutorial but for now, the following will hopefully give you a sense of what is happening.
How is a voltage divider connected?
A single resistor is wired between the outside legs of the pot. The size if this resistor is fixed and stamped on the body of the pot — 10K or 500K for the ones in our kit. The middle leg — called the wiper — is connected to the dial on the pot top ( couldn’t resist — it’s a palindrome!). The wiper cuts the pot’s resistor into two. nAs you turn the dial, you adjust the position of the wiper, changing the proportion of resistance to its left and right. In other words, the wiper divides the internal resistance.
Because voltage is related to resistance ( Ohms’ law) we see the impact of the resistance changes as changes in voltage.
A POT thought-experiment
Let’s think about how electricity flows in this circuit. In particular let’s think about how that 470 ohm resistor protects the LED and impacts current flow.
POT TURNED ALL THE WAY TO POSITIVE
The 470 ohm resistor is necessary when the pot is turned all the way towards the side connected to power +Ve (high side of water fall). In this state, two things happen. First, the resistance between the power supply and the wiper drops to zero (all 10k are on the other side of the wiper). Second, this allows the full voltage of the supply to flow out the wiper. If we did not include the 470ohm resistor the LED would have zero resistance in its circuit and would get full voltage –> that almost always means an immediately dead LED. IF you are lucky it will pop and the cap will shoot off (watch your eyes! — I have actually only seen this once). Most likely it will just melt, possibly making a wimpering pop and hiss sound and then smell really bad.
POT TURNED ALL THE WAY TO NEGATIVE
Now, image we turn the wiper all the way the other way — so that there is 10K ohms between +Ve and wiper. And zero resistance between the wiper and ground. At the wiper the electrical flow has two paths it can follow (ie the circuit branches are in parallel). Current can go through the zero resistance path to the battery ground or it can go through the LED and the 470 ohm resistor. Recall that electricity ALWAYS follows the path of least resistance. The current will all go the zero resistance route and the LED will no longer light up.
Going Further
There are lots of sites with info on pots and voltage dividers. Here are a couple to get you started.
voltage divider this one has a great explanation of voltage dividers.
Sparkfun tut on pots. A goog intro.
Some Khan Academy Theory. This one is WAY deeper than we need to go — but if you are into this sort of thing — go for it. I already regret sharing it a little.
Analog Outputs
Analog Outputs (AO) are one of the four contexts or types of circuits that are core to tangible.
If you have not yet looked at the tangible matrix Building Block, you should check it out now. It will introduce you to all four contexts.
Context :: Analog Output
Analog refers to signals, circuits, or logical systems that are variable or graded. They can be the top or bottom of the waterfall and every shade in between.
Input and Output are defined from the perspective of our Arduino.
Electrical signals generated by the Arduino and sent out to circuits are OUTPUTS.
So, Analog Outputs are variable signals (V) that move OUT of the Arduino.
PWM — How Digital Becomes Analog
The Arduino Uno is a strictly digital device. Given this, how is it going to make an analog signal?
The Arduino approximates analog output signals using a process called pulse width modulation or PWM. PWM is achieved by turning a selected pin HIGH and LOW very, very quickly. By altering ON (HIGH, 1) time and OFF (LOW, 0) time one can alter average current delivered to a circuit. We call the ratio of ON:OFF time the duty cycle.
Duty Cycle: A deeper look at PWM
We can use a concept called duty cycle to imagine how this works. A deep look at duty cycle is beyond what we need at this level — but I will cover it in general here. Follow links below for details.
First imagine an LED blinking on a digital pin. The duration of time the pin is HIGH (1) is called ‘ON’ time. And conversely, the amount of time the pin spends ‘LOW” (0) is known as ‘OFF’ time. The blink can be asymmetrical, it can be ON for more or less time than it is ‘OFF”.
We will call one cycle the amount of time it takes to go from the start of the HIGH, through the LOW and just back to the start of the next HIGH.
The percentage of time spent in the HIGH state during one cycle is called the duty cycle.
So a 50% duty cycle would be HIGH (on) for half the time and OFF for the other half. A 75% duty cycle would be on 3/4 of the time and off for 1/4.
A 100% duty cycle is the same as setting a pin HIGH while 0% is equivalent to settinga pin LOW.
Read more about duty cycle and PWM here.
PWM: Controlling LEDs and Motors
The pulses of a PWM cycle are incredibly fast (brief) — the individual blinks are too fast to see with the naked eye. But they are very real.
When an LED is powered by a pin using PWM, reductions in duty cycle reduce LED brightness. The outcome is a an ON/OFF cycle that dims an LED. This effect is grounded in the same principle that makes video and film look like moving images — not stuttering stills.
We will also use PWM to control speed of motors and the positions of servos.
Card
Your kit contains a reference card for analog outputs.
Each card indicates the context, command, circuit and offers a code sample.
Video :: Analog Outputs
Get the Slides
Classic Example
The classic example of a analog output is a fading LED.
Circuit
The classic circuit for exploring an analog output is a simple LED and current limiting resistor. Yup — it is the same circuit we used for digital outputs.
Command :: analogWrite()
The code that creates an Analog Output is:
analogWrite(pin, state);
where:
pin = 3,5,6, 9,10,11
state = 0 – 255
Note 1: these pins are marked with a tilde ~
The line of code above means:
Use PWM to set the duty cycle of ~pinX to state = 0 to 255. Where 0 = 0% and 255= 100% duty cycle.
How Does it Work?
As indicated above, analogWrite quickly blinks ~pinX. The variable state defines the duty cycle.
The result is a staircase like change in brightness as state increases from 0 to 255.
Important in Arduino, we Write outputs.
Check the Arduino Docs for this topic
Code Sample
int ledPIN = 6;
int state = 0;
void setup() {
pinMode( ledPIN, OUTPUT );
}
void loop() {
analogWrite( ledPIN, state );
state++;
delay(50);
}
Once you have this basic code running, try also cycling through the range of possible states with a for loop instead of the counter.
Going Further
Read more about duty cycle and PWM from sparkfun.
Lots of in depth (too much?) info on PWM at wikipedia
Some follow up Arduino references:
Analog Inputs
Analog Inputs (AI) are one of the four contexts or types of circuits that are core to tangible.
If you have not yet looked at the tangible matrix Building Block, you should check it out now. It will introduce you to all four contexts.
Context :: Analog Input
Analog refers to signals, circuits, or logical systems that are variable or graded. They can be the top or bottom of the waterfall and every shade in between.
Input and Output are defined from the perspective of our Arduino.
Electrical signals generated in circuits and sent into the Arduino are INPUTS.
So, Analog Inputs are variable signals (V) that move INTO the Arduino.
Card
Your kit contains a reference card for analog inputs.
Each card indicates the context, command, circuit and offers a code sample.
Video :: Analog Inputs
Get the Slides
Classic Example
At the introductory level, two circuits can be used as classic examples of an analog input: the POTentiometer, or POT and a photoCell.
Circuit
POTs and photoCells are both resistive circuits that make use of a concept called a voltage divider. The electrical similarity of these examples means they can be swapped out and the core behaviours will remain the same. The Analog Input card in your kit depicts a photocell circuit. We will look at both in this Building Block.
Potentiometer Circuit
The circuit for reading a potentiometer is very similar to the basic POT setup. In this case we will use the Arduino 5V for power (not your battery!) and connect the POT wiper to an Analog IN pin.
Photocell Circuit
As above, the basic photocell circuit is modified to connect to an Analog In pin.
Command :: analogRead()
The code that creates an Analog Input is:
state = analogRead(pin);
where:
pin = A0 – A5 (inclusive)
state = 0 – 1023
Note 1: these pins are prefixed with a capital A
The line of code above means:
Read the analog (variabel, staircase) state of pinAx and store that reading in a variable called state.
How Does it Work?
Much like we did for Analog Outputs we can imagine the input signal as a staircase of changing voltages.
The steps on the staircase of an Analog Input are much shorter than their output sibling, so even though we still cover a range of 5V, there are 4x as many steps! The state range for Analog Input therefore is 0 – 1023. (Note: analog inputs store 10-bit numbers while analog outputs use 8-bit numbers).
As mentioned above, both examples belong to a class of circuits known as voltage dividers.
When two resistors are placed in series with one another and we measure the voltage at the point between them we see a voltage that falls between max power (top of waterfall, 5V in Arduino Uno) and ground (bottom of waterfall, 0V). The exact voltage depends on the ratio of the resistors.
POTentiometer
The wiper in the POT, acts to continuously divide the total resistance of the device. We there for have two resistors that change in opposition to each other. When one gets big, the other gets small. Technically we say that they are in an inverse relationship.
When you turn the dial of the POT all the way towards the leg connected to ground you make the resistance between 5V and the wiper relatively LARGE. The resistance between wiper and ground (0V) gets close to zero. In this case the signal to the Arduino is small and the value (state) of an analogRead is close to zero.
When you turn the dial of the POT all the way towards the leg connected to 5V the resistance between 5V and the wiper gets close to zero. The resistance between wiper and ground gets relatively LARGE. In this case the signal to the Arduino is close to 5V and the value (state) of an analog read is close to the upper limit of 1023.
PhotoCell
In the case of the photocell we have one fixed resistor and one variable resistor that depends on light.
When the environment is dim, the resistance of the photoCell is HUGE ( ~1 Million Ohms). In this case the signal to the Arduino is small and the value (state) of an analog read is close to zero.
When the environment is bright, the resistance of the photoCell drops rapidly (~1k or less in bright light). In this case the signal to the Arduino is close to 5V and the value (state) of an analog read is close to the upper limit of 1023.
Important
In Arduino, we read inputs.
Check the Arduino Docs for this topic
Code Sample
Connect your circuit to pin A0. Upload the code below. Open the Serial Plotter.
With a photocell circuit; cover the sensor with your hand watch the plotter trace. With the POT circuit; turn the dial, watch the trace. Notice that the POT holds it value.
int sensorPin = A0;
int state = 0;
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}
void loop() {
state = analogRead( sensorPin );
Serial.println(state);
delay(50);
}
What to expect
The trace below is a screen cap from my serial plotter while running the above code with a photocell connected. The relatively flat parts of the curve on the left and right are from moments when full room (ambient) light fell on the sensor.
The wobbly bit in the middle is from me quickly covering and uncovering the sensor with my hand.
Give it a try.
Going Further
Some follow up Arduino references:
*see analogInputs in action with a photocell or a pot
*learn about thresholding analog sensor values.
Digital Outputs
Digital Outputs (DO) are one of the four contexts or types of circuits that are core to tangible.
If you have not yet looked at the tangible matrix Building Block, you should check it out now. It will introduce you to all four contexts.
Context :: Digital Output
Digital refers to signals, circuits, or logical systems that only have two states; ON, OFF.
Input and Output are defined from the perspective of our Arduino.
Electrical signals generated by the Arduino and sent out to circuits are OUTPUTS.
So, Digital Outputs are ON/OFF signals (V) that move OUT of the Arduino.
Card
Your kit contains a reference card for digital outputs.
Each card indicates the context, command, circuit and offers a code sample.
Video :: Digital Outputs
This first video is from a project that I completed with David Bouchard. An alternate course specific video can be found below.
tangible introduction to digital outputs. Please note — I extend the first build with an RGB LED in this video. If you have not looked at RGB LED you can just use 2 single color LEDs. It simplifies the circuit. I will post an RGB video ASAP.
Code samples from the video can be found below.
Get the Slides
Classic Example
The classic example of a digital output is a blinking LED.
Circuit
The classic circuit for testing a digital output is a simple LED and current limiting resistor. Circuit on left when state = 0, LOW, circuit on right when state = 1,HIGH.
Command :: digitalWrite()
The code that creates a Digital Output is:
digitalWrite(pin, state);
where:
pin = 2 – 13 (inclusive)
state = LOW (0, Ground) OR HIGH (1, 5V)
Note 1: pin can also include 0,1; but these are reserved for communication, so best to avoid them. Note 2: pin 13 has the built in LED attached
The line of code above means:
Write a digital (on/off) signal to pin at level = state (0/LOW or 1/HIGH).
How Does it Work?
If a 1 or HIGH is written, then the voltage (V) at the pin is set to 5V.
If a 0 or LOW is written, then the voltage (V) at the pin is set to 0V (ground).
Important
In Arduino, we Write outputs. Image creating waterfalls on the selected pin.
Check the Arduino Docs for this topic
Code Sample – SINGLE LED
int ledPin = 5 ; // led on pin 5
void setup() {
pinMode ( ledPin , OUTPUT) ; // set direction
}
void loop() {
digitalWrite ( ledPin , 1 ) ; // turn LED on
delay ( 1000 ) ; // wait a bit == 1 second
digitalWrite ( ledPin, 0 ) ; // turn LED off
delay ( 1000 ) ; // wait a bit
}
CODE Sample – 2 colors of RGB LED
// code sample using B and R, of RGB led
int blueLED = 5 ; // connect PIN 5 to BLUE pin of RGB LED
int redLED = 10 ; // connect PIN 10 to RED pin of RGB LED
void setup() {
// put your setup code here, to run once:
pinMode ( blueLED , OUTPUT) ;
pinMode ( redLED , OUTPUT) ;
}
void loop() {
// put your main code here, to run repeatedly:
// blue
digitalWrite ( blueLED , HIGH ) ; // turn LED on
delay ( 1000 ) ; // wait a bit == 1 second
digitalWrite ( blueLED, LOW ) ; // turn LED off
delay (1000 ) ; // wait a bit
// red
digitalWrite ( redLED , HIGH ) ;
delay ( 1000 ) ; // wait a bit == 1 second
digitalWrite ( redLED, LOW ) ; // turn LED off
delay (1000 ) ; // wait a bit
}
Going Further
Some follow up Arduino references:
Digital Inputs
Digital Inputs (DI) are one of the four contexts or types of circuits that are core to tangible.
If you have not yet looked at the tangible matrix Building Block, you should check it out now. It will introduce you to all four contexts.
Context :: Digital Input
Digital refers to signals, circuits, or logical systems that only have two states; ON, OFF.
Input and Output are defined from the perspective of our Arduino.
Electrical signals generated in circuits and sent into the Arduino are INPUTS.
So, Digital Inputs are ON/OFF signals (V) that move INTO the Arduino.
Card
Your kit contains a reference card for digital inputs.
Each card indicates the context, command, circuit and offers a code sample.
Video :: Digital Inputs
Get the Slides
Classic Example
The classic example of a digital input is a button.
Circuit
The classic circuit for testing a digital input is a simple push button (tho any two wires could do). The circuit on left indicates electrical state (state = 0, LOW) when a button is unpressed . The circuit on right indicates electrical state (state = 1,HIGH) when button is pressed.
Command :: digitalRead()
The code that creates a Digital Input is:
state = digitalRead(pin);
where:
pin = 2 – 13 (inclusive)
state = 0 OR 1 (gnd or 5V)
Note 1: pin can also include 0,1; but these are reserved for communication, so best to avoid them.
Note 2: pin 13 has the built in LED attached
The line of code above means:
Read the digital (on/off) state of pinX and store that reading in a variable called state.
How Does it Work ?
If the button is pressed, a 5V signal reaches pinX and a 1 ( conceptually a HIGH) is stored in state.
If the button is not pressed (released), a 0V or GND signal reaches pinX and a 0 ( conceptually a LOW) is stored in state.
Important
In Arduino, we read inputs.
Check the Arduino Docs for this topic
Code Sample
int buttonPIN = 12;
int state = 0;
void setup() {
pinMode( buttonPIN, INPUT );
Serial.begin(9600);
}
void loop() {
state = digitalRead( buttonPIN );
Serial.println(state);
}
Going Further
Some follow up Arduino references: