Lesson 2 Digitial input and output
1.Components for the lab
LEDs (Light emiting diodes)
2.Recall: Our First Program
The program contains one variable, two user define functions, and involved three pre-defined functions. Below we will explain them one by one.
function setup() – this function runs once when the program starts, usually we will put all initialization code here. For example, setting the pin mode and initializing the libraries and objects we used in the program.
function loop() – this function runs over and over again (after the setup() function is called), as long as the Arduino board has power.
Note that on the Arduino board there is a reset button, you can use it to terminate the current running program and restart the program (i.e. setup() will run once and than loop() will repeated over and over).
variable ledPin – the pin number we set as output (pin 13, check your board). Note that pin 13 is a special pin as it has its own LED on the board. When you set pin 13 is output pin and set it with a HIGH voltage, the LED on the board is on. Also this pin has build-in 1K resistor, so you can plug an LED from pin13 to GND directly without burning it.
Important note: if you set other pins as output, remember to use resistors in the circuit, otherwise it will burn the components because other pins do not have build-in resistor.
function pinMode() – this function is used to set the pin mode for digital pins (0-13). Two pin mode are possible: either INPUT or OUTPUT.
function delay() – this function pauses the program for the amount of time (in miliseconds) (1000 ms = 1s)
function digitalWtite() – this function writes a HIGH (5V) or a LOW (0V, ground) value to a digital pin.
3.How to Connect Things Using Breadboard
The top two rows (and bottom two rows) are usually used for providing power and ground for all components on the board.
The middle of the board is where you connect components and controllers togethers. Each row can be connected to the other using jumper wires or 24-gauge wire.
Similar to digital output, digital input only consider HIGH or LOW voltages. In the following we will use switch to control the LEDs, by checking the voltages of the current is closed or opened.
In short, we have two ways to use the switch (generate HIGH and LOW inputs):
Positive Logic – low voltage for a 0 bit (OFF) and high voltage for a 1 bit (ON)
Negative Logic – high voltage for a 0 bit (OFF) and low voltage for a 1 bit (ON).
Imagine the measure point is connect to ground with very high resistance, therefore we can measure the voltage with only a very small current flow through the measure wire. In the workshop we usually use the positive logic, becasue it is more easy to understand.
To read signal from a pin of the Arduino board, we need to set the pin mode to INPUT and read the digital signal
void pinMode(int, mode) : set the pin mode
int digitalRead(int) : read the pin
5.Using a Pushbutton to Control the LED
6.Exercice – Tilt switch
Tilt switches can be used to detect orientation and vibration, there are different types of tilt switches, e.g. ball rolling switch, mercury tilt switch, and photo interrupt switch.
Taks: Replace the switch button with the tilt switch, and use this to control the on/off state of the LED on pin 13.
7.One Circult, Different Behaviours
The main advantage of using programmable microcontroller over calssic electronics is that we can change the behaviours of the circult WITHOUT changing the circult itself. By replacing the program uploaded to Arduino, we can easily change the responses of your circult.
Below is an example that turns on (off) the LED with the button is pressed and keeps it on (off) after the button is released.
The above code seems correct, but you will find that this does not always work when you uploaded to your board. It just because pressing the button once would appear to the code as multiple presses. Using of the delay() function can filter the wrong jumps between HIGH and LOW voltages.
Task: Modify the program so that the LED is on when every four button pushes. (i.e. on – off – off – off – on – off – off – off …)
8.Example: Controlling Two LEDs
Task: Control two LEDs using a switch (pressed – red, release – green).
9.Exercise – LED Array
demo program: blinking 2 LEDs with different frequency
demo program: blink 6 LEDs with 3 pins
Extra Task: Control 6 LEDs (display pattern) with 3 pins only
Task 1: Design your LED blinking pattern with 5 (or more) LEDs. [example pattern with 4 LEDs]
Hints on programming:
- Array of output pins.
- Use for loop to change the states of LEDs.
Task 2: Control the LEDs use a switch button, each time only one LEDs is on, when the button is pressed, the next LED is on and the current LED is off.
Multiplexed displays are electronic displays where the entire display is not driven at one time. Instead, sub-units of the display (typically, rows or columns for a dot matrix display or individual characters for a character orientated display, occasionally individual display elements) are multiplexed, that is, driven one at a time, but the electronics and the persistence of vision combine to make the viewer believe the entire display is continuously active.
2n pins can control n^2 LEDs!
A multiplexed display has several advantages compared to a non-multiplexed display:
- Fewer wires (often, far fewer wires) are needed
- Simpler driving electronics can be used
- And both lead to reduced cost
- Reduced power consumption
(from wikipedia: http://en.wikipedia.org/wiki/Multiplexed_display)
Extra Task: Control 9 LEDs (display pattern) with 6 pins only
Charlieplexing is a technique proposed in early 1995 by Charlie Allen at Maxim Integrated Products for driving a multiplexed display in which relatively few I/O pins on a microcontroller are used to drive an array of LEDs. The method utilizes the tri-state logic capabilities of microcontrollers in order to gain efficiency over traditional multiplexing. Although it is more efficient in its use of IO, there are issues that cause it to be more complicated to design and render it impractical for larger displays. These issues include duty cycle, current requirements and the forward voltages of the LEDs.
n pins can control n^2 – n LEDs!
(from wikipedia: http://en.wikipedia.org/wiki/Charlieplex)