Arduino Examples
Arduino Examples
Arduino Examples
Table of Contents
ARDUINO MICROCONTROLLER..............................................................................................2
Example 1: BLINK..........................................................................................................................3
Example 2: KNIGHT RIDER..........................................................................................................4
Example 3: FADE............................................................................................................................5
Example 4: RGB LED.....................................................................................................................6
Example 5: ANALOG READ..........................................................................................................8
Example 6: ANALOG-IN DIGITAL-OUT VOLTAGE METER....................................................9
Example 7: 3-BIT DIGITAL READ..............................................................................................10
Example 8: SEVEN SEGMENT DISPLAY..................................................................................12
Example 9: 2-DIGIT COUNTER..................................................................................................13
Example 10: LIGHT METER........................................................................................................15
ARDUINO MICROCONTROLLER
Arduino is an open-source platform used for building electronics prototypes. Arduino
encompasses both the hardware (a physical programmable circuit board - microcontroller)
and the software (IDE used to write and upload computer code to the physical board)
Arduino has become popular due to the ease of use, price, and modules available.
Furthermore, the presence of a vibrant group of users who are enthusiastic about the use
and sharing of arduino code.
Arduino manufacture several types of boards, which vary in size and functionality. We will
be using the Arduino Uno board mostly.
Pin-layout of Arduino-UNO
Example 1: BLINK
This examples shows how to set up a simple arduino circuit.
Learning outcomes: (1) setting digital outputs (2) Infinite loops (3) Delay function
Requirements:
1 Arduino board (any one)
1 LED
1 Resistor (1 kΩ, 270 Ω or 330 Ω )
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
Further practice:
1) Create a sketch which produces a “S.O.S.” blink pattern using Morse code
“...” S “___” O “...”
Example 2: KNIGHT RIDER
This examples assembles a rail of 5 LEDs to scroll up and down like the popular 80s TV
show “Knight Rider”.
Learning outcomes: (1) arrays (2) for loops
Requirements:
1 Arduino board (any one)
5 LED
5 Resistors (1 kΩ, 270 Ω or 330 Ω )
void setup() {
// we make all the declarations at once
for (count=0;count<5;count++) {
pinMode(pinArray[count], OUTPUT);
}
}
void loop() {
for (count=0;count<5;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
}
for (count=4;count>=0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
}
}
Further practice:
1) Setting up an 8 LED rail
2) Scrolling two LEDs at one time
3) Smoothly fading in and out (note: requires PWM, do after next example)
Example 3: FADE
This examples shows how to fade a LED light on and off smoothly.
Learning outcomes: (1) setting digital outputs (2) Infinite loops (3) Delay function
Requirements:
1 Arduino board (any one)
1 LED
1 Resistor (1 kΩ, 270 Ω or 330 Ω )
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
Further practice:
1) Change the code to fade quicker or faster using delay
2) Modify the code to not crash when the fadeAmount is set to an arbitrary value
Example 4: RGB LED
This examples shows how to show an arbitary colour using a 3-color LED.
Learning outcomes: (1) functions
Requirements:
1 Arduino board (any one)
1 RGB three colour LED (diagram and code using a common cathode variety)
3 Resistors (1 kΩ, 270 Ω or 330 Ω )
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
setColor(255, 0, 0); // Red
delay(1000);
setColor(0, 255, 0); // Green
delay(1000);
setColor(0, 0, 255); // Blue
delay(1000);
setColor(255, 255, 255); // White
delay(1000);
setColor(255, 0, 255); // Purple
delay(1000);
setColor(255, 255, 0); // Yellow
delay(1000);
setColor(0, 255, 255); // Cyan
delay(1000);
setColor(255, 127, 0); // Yellow
delay(1000);
setColor(255, 63, 0); // Orange
delay(1000);
}
Fix three digital outputs to the LEDs through the resistors. Make sure to use PWM enabled
pins
Further practice:
1) Using 3 variable resistors instead of fixed resistors to balance colors properly
2) Modify the code to fade through colours smoothly
Example 5: ANALOG READ
This examples shows how to read in an analog voltage.
Learning outcomes: (1) Analog Read (2) ADC
Requirements:
1 Arduino board (any one)
1 Variable resistor (Potentiometer)
1 LED
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}
Example 6: ANALOG-IN DIGITAL-OUT VOLTAGE METER
This examples shows how to read in an analog voltage.
Learning outcomes: (1) Serial Monitor
Requirements:
1 Arduino board (any one)
1 Variable resistor (Potentiometer)
4 LED
4 Medium resistors (270, 330, 1 k)
void loop() {
val = analogRead(potPin); // read the value from the sensor
//Serial.print(val);
//Serial.print("\t");
//Serial.print(level);
//Serial.print("\n");
level = val/(1024/5);
for (int i=0; i< level; i++) {
digitalWrite(leds[i], HIGH);
}
delay(100);
for (int i=0; i< 5; i++) {
digitalWrite(leds[i], LOW);
}
}
Example 7: 3-BIT DIGITAL READ
This examples shows how to read in an analog voltage.
Learning outcomes: (1)
Requirements:
1 Arduino board
3 Push button
3 LEDs
3 Medium resistors (270, 330, 1 k)
void setup () {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
Serial.begin(9600);
}
void loop () {
delay(100);
// read the pushbutton state
bit1 = digitalRead(button1);
bit2 = digitalRead(button2);
bit3 = digitalRead(button3);
if(bit1 != bit1last ||
bit2 != bit2last ||
bit3 != bit3last) {
value = bin2dec(bit3, bit2, bit1);
Serial.print("\n"); Serial.print(bit1);
Serial.print("\t"); Serial.print(bit2);
Serial.print("\t"); Serial.print(bit3);
Serial.print("\t Value: "); Serial.print(value);
bit1last = bit1;
bit2last = bit2;
bit3last = bit3;
}
}
Setup
G DP A F CA2
1) First need find the segments of the display
Connect CA1 and CA2 separately through 1 k resistors to 5 V to test the display. Then test
each segment through grounding the relevant pins.
2) Connect the display to the arduino board. CA1, CA2 and pins for segments A – G
should be connected to digital output pins.
#define CA1 13
#define CA2 12
#define A_SEG 1
#define B_SEG 2
#define C_SEG 3
#define D_SEG 4
#define E_SEG 5
#define F_SEG 6
#define G_SEG 7
N\Seg Bin G F E D C B A
0 0b 1 0 0 0 0 0 0
1 0b 1 1 1 1 0 0 1
2 0b 0 1 0 0 1 0 0
3 0b 0 1 1 0 0 0 0
4 0b 0 0 1 1 0 0 1
5 0b 0 0 1 0 0 1 0
6 0b 0 0 0 0 0 1 0
7 0b 1 1 1 1 0 0 0
8 0b 0 0 0 0 0 0 0
9 0b 0 0 1 0 0 0 0
Example 10: LIGHT METER
This examples shows how to read in an analog voltage.
Learning outcomes: (1)
Requirements:
1 Arduino board
1 Light Dependant Resistor
1 Variable Resistor
1 5 k Resistor
4 LEDs
4 Medium resistors (270, 330, 1 k)
1
kΩ