Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

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 Ω )

int pinArray[5] = {2, 3, 4, 5, 6}; // pins which LEDs are connected


int count = 0; //
int timer = 150; // how fast the light slides across

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 Ω )

int led = 9; // the PWM pin the LED is attached to


int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
// set the brightness of pin 9:
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);
}

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 Ω )

PART 1 – Finding colours and assigning pins


Fix three digital outputs to the LEDs through the resistors. Make sure to use PWM enabled
pins
void setup() {
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}

void loop() { digitalWrite(3, HIGH);


delay(1000);
digitalWrite(3, LOW);
delay(1000);
digitalWrite(5, HIGH);
delay(1000);
digitalWrite(5, LOW);
delay(1000);
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
delay(1000);
}
PART 2 – Setting up Looping code
int redPin= 6; //pwm pin
int greenPin = 5; // pwn pin
int bluePin = 3; // pwn pin

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void setColor(int redValue,


int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}

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

int potPin = 0; // select the input pin for the potentiometer


int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the
sensor

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)

int potPin = 0; // select the input pin for the potentiometer


int val = 0; // variable to store the value coming from the sensor
int leds[5] = {3, 4, 5, 6, 7};
double level;
void setup() {
for (int i=0; i< 5; i++) {
pinMode(leds[i], OUTPUT);
}
Serial.begin(9600);
}

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)

Setup: setup three push buttons through resistors and LEDs


Use three digital pins to probe the voltage after the push buttons.

boolean bit1 = false;


boolean bit1last = false;
boolean bit2 = false;
boolean bit2last = false;
boolean bit3 = false;
boolean bit3last = false;
int button1 = 8;
int button2 = 9;
int button3 = 10;
int value = 0;

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;
}
}

// convert 3-bit binary number to decimal


int bin2dec (int b1, int b2, int b3) {
int decimal_value = 1*b3 + 2*b2 + 4*b1;
return(decimal_value);
}
Example 8: SEVEN SEGMENT DISPLAY
Example 9: 2-DIGIT COUNTER
CA1 B C E D

This examples shows how to read in an analog voltage.


Learning outcomes: (1)
Requirements:
1 Arduino board
1 2-digit seven segment display

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

// Pins for A B C D E F G, in sequence


const int segs[7] = { A_SEG, B_SEG, C_SEG, D_SEG, E_SEG, F_SEG,
G_SEG };
// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100,
0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };
void setup() {
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A_SEG, OUTPUT);
pinMode(B_SEG, OUTPUT);
pinMode(C_SEG, OUTPUT);
pinMode(D_SEG, OUTPUT);
pinMode(E_SEG, OUTPUT);
pinMode(F_SEG, OUTPUT);
pinMode(G_SEG, OUTPUT);
}
void loop() {
for (int digit1=0; digit1 < 10; digit1++) {
for (int digit2=0; digit2 < 10; digit2++) {
for (int d=0; d<100; d++){
digitalWrite(CA1, HIGH);
digitalWrite(CA2, LOW);
lightSegments(numbers[digit1]);
delay(10);
digitalWrite(CA2, HIGH);
digitalWrite(CA1, LOW);
lightSegments(numbers[digit2]);
delay(10);
}
}
}
}

void lightSegments(byte number) {


for (int i = 0; i < 7; i++) {
int bit = bitRead(number, i);
digitalWrite(segs[i], bit);
}
}

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Ω

int maxLight = 90;


int minLight = 870;
int division = (maxLight – minLight)/4;
void setup() { Serial.begin(9600); pinMode(6, OUTPUT);
pinMode(5, OUTPUT); pinMode(4, OUTPUT); pinMode(3, OUTPUT);
}
void loop() {
int sensorValue = analogRead(A0); // Convert the analog reading
(which goes from 0 - 1023) to a voltage (0 – 5V):
float voltage = sensorValue * (5.0 / 1023.0); // print out the
value you read:
Serial.println(sensorValue);
if(sensorValue<minLight) {digitalWrite(6, HIGH);} else
{ digitalWrite(6,LOW);}
if(sensorValue<(minLight+division)) {digitalWrite(5, HIGH);}
else { digitalWrite(5, LOW);}

if(sensorValue<(minLight+2.0*division)) {digitalWrite(4, HIGH);}


else {digitalWrite(4, LOW);}
if(sensorValue<(minLight+3.0*division)) {digitalWrite(3, HIGH);}
else { digitalWrite(3, LOW);} delay(500);}

You might also like