You are on page 1of 25

Hawassa University, Institute of Technology School of

Electrical and Computer Engineering

Embedded Systems (ECEg-5702)

Arduino Programming

By Muluneh H
16/04/2018 1
Outline of the Lecture

Arduino

Arduino Code
• Blink LED
• Digital Read Serial
• Read Analog Voltage
• Interfacing DC motor
• Interfacing Steeper motor
• Interfacing GSM
• Interfacing GPS
• Interfacing LCD

Summary

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 2
Arduino
• Arduino is an open-source electronics prototyping
platform based on flexible, easy-to-use hardware
and software.
• It's intended for designers, hobbyists, and anyone
interested in creating interactive objects.
• Made in Italy
• Arduino is an open-source single microcontroller board based on flexible and easy-to-
use hardware and software.
• Basically, the hardware consists of an Atmel AVR processor, USB interface, onboard
programmer and input/output support.
• Based on Atmel microcontroller
• There are different version of Arduino, with additional components and analog/digital
ports.
• The software consists of a standard programming language compiler (C or
C++), that includes a special library called "Wiring" which makes the most
common input/output operations very easy, and the boot loader that runs on
the board. You can find the language code reference and documentation on
the official Arduino website:
http://www.arduino.cc/

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 3
Arduino Boards
• Arduino can sense the environment by receiving input from a
variety of sensors and can affect its surroundings by
controlling lights, motors, and other actuators.
• The microcontroller on the board is programmed using the Arduino
programming language and the Arduino development environment.
• Arduino projects can be standalone or they can communicate with
software running on a computer.
• The boards can be built by hand or purchased preassembled; the
software can be downloaded for free.
• The hardware reference designs (CAD files) are available under
an open-source license, you are free to adapt them to your needs.

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 4
Arduino Uno
• The Arduino Uno is a board based on the ATmega328 microcontroller.
• It has 14 digital input/output pins (of which 6 can be used as PWM
outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB
connection, a power jack, an ICSP header, and a reset button.

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 5
Getting Starting 1
• The Arduino board needs a standard USB cable for the
programming of the microcontroller and for getting power (the
green power LED should go on).

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 6
Getting Starting 2
• Download and install the open-source (http://arduino.cc/en/Main/Software)
Arduino environment software to write code and upload it to the Arduino
board.
• It runs on Windows, Mac OS X, and Linux. The environment software is
written in Java.

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 7
Arduino - Get started

• Let’s start using the Arduino board:


• Connect the Arduino board to the PC using the provided USB cable.
• Install the Arduino driver as indicated in
http://www.arduino.cc/en/Guide/Windows.
• Launch the Arduino application and configure your setup and the
serial port as indicated in http://www.arduino.cc/en/Guide/Windows.

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 8
Arduino Code

• The Arduino code consists of two parts:


the setup() and
the loop().
• The setup() function is called only when the Arduino is powered on or
reset.
Use it to initialize variables, pin modes, start using libraries, etc.
The setup function will only run once, after each power up or reset of the
Arduino board.
• The loop() function runs continuously till the device is powered off. .
Code in the loop() section of your sketch is used to actively control the Arduino
board.
The main logic of the code goes here. Similar to while (1) for micro-controller
programming.
• Any line that starts with two slashes (//) will not be read by the
compiler, so you can write anything you want after it.
Commenting your code like this can be particularly helpful in explaining, both to
yourself and others, how your program functions step by step.

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 9
Bare minimum code

void setup() {
// put your setup code here, to run once:
}

void loop() {
// put your main code here, to run repeatedly:
}

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering
PinMode
• A pin on arduino can be set as input or output by using pinMode()
function.
• Example:
pinMode(13, OUTPUT); // sets pin 13 as output pin

pinMode(13, INPUT); // sets pin 13 as input pin

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 11
Reading/writing digital values

digitalWrite()
digitalRead()

• Example
digitalWrite(13, LOW); // Makes the output voltage on pin 13 , 0V

digitalWrite(13, HIGH); // Makes the output voltage on pin 13 , 5V

int buttonState = digitalRead(2); // reads the value of pin 2 in buttonState

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering
Analog to Digital Conversion

• What is analog ? It is continuous range of voltage values (not just 0


or 5V)
• Why convert to digital ? Because our microcontroller only
understands digital.
• The Arduino Uno board contains 6 pins for ADC
10-bit analog to digital converter
This means that it will map input voltages between 0 and 5 volts into integer
values between 0 and 1023

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering
Reading/Writing Analog Values
• analogRead(A0); // used to read the analog value from the pin A0

• analogWrite(2,128);

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering
Blink LED
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// 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
}
Hawassa University, Institute of Technology School of
Electrical and Computer Engineering 16/04/2018 15
Digital Read Serial
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
*/
// let digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1000); // delay in between reads for stability
}

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 16
Analog Read Serial
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 17
Read Analog Voltage
/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
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(voltage);
delay(1000);
}

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 18
Read a character and on/off a led
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
void setup() {
pinMode(led, OUTPUT);// initialize the digital pin as an output.
Serial.begin(9600);// Open serial communications and wait for port to open:
while (!Serial) {; // wait for serial port to connect. }
Serial.print("Send me 0 to swich off the led and 1 to swich on: \n");
}
void loop() {
// get any incoming bytes:
if (Serial.available() > 0)
{
int thisChar = Serial.read();
if(thisChar=='0'){
Serial.print("Led OFF: \'");
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
}
else{
Serial.print("Led ON: \'");
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
}
Serial.print("Send me 0 to swich off the led and 1 to swich on: \n");
}
}
Hawassa University, Institute of Technology School of
Electrical and Computer Engineering 16/04/2018 19
Interfacing DC Motor
const int pwm = 3 ; //initializing pin 3 as pwm void loop()
const int in_1 = 8 ;
{
digitalWrite(in_1,HIGH) ; //For Clock wise
const int in_2 = 9 ;
motion , in_1 = High , in_2 = Low
//For providing logic to L293d IC to choose the direction of the DC motor
digitalWrite(in_2,LOW) ;
void setup
analogWrite(pwm,200) ;
{ delay(3000) ; //Clockwise for 3 secs
//we have to set PWM pin as output digitalWrite(in_1,LOW) ; //For brake
pinMode(pwm,OUTPUT) ; digitalWrite(in_2,LOW) ;
//Logic pins are also set as output delay(1000) ;
digitalWrite(in_1,LOW) ; //For Anti Clock-
pinMode(in_1,OUTPUT) ;
pinMode(in_2,OUTPUT) ; wise motion - IN_1 = LOW , IN_2 = HIGH
}
digitalWrite(in_2,HIGH) ;
delay(3000) ;
/*setting pwm of the motor to 255
digitalWrite(in_1,LOW) ; //For brake
we can change the speed of rotaion
digitalWrite(in_2,LOW) ;
by chaning pwm input but we are only
delay(1000) ;
using arduino so we are using higest
}
value to driver the motor */
Hawassa University, Institute of Technology School of
Electrical and Computer Engineering 16/04/2018 20
L293D
• L293D is a typical motor driver IC.
• L293D works on concept of H-bridge.
• In H-bridge, Voltage is allowed to be
flown in either direction because
voltage needs to change its direction
to make the motors to move in either
clockwise or anticlockwise.
• In single chip of L293D, there are two
bridges and so, two DC motors can
work here independently at the same
time.
• It provides bidirectional current up
to 600mA.

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 21
H-Bridge

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 22
PWM

• PWM, or pulse width modulation is a technique which allows us


to adjust the average value of the voltage that’s going to the
electronic device by turning on and off the power at a fast rate.
• The average voltage depends on the duty cycle, or the amount of
time the signal is ON versus the amount of time the signal is OFF
in a single period of time.

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 23
PWM

• So By controlling the rms input voltage we can control the speed


of the motor

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 24
Interfacing Steeper Motor
• Stepper motors fall somewhere in between a regular DC motor and
a servo motor.
• They have the advantage that they can be positioned accurately,
moved forward or backwards one 'step‘at a time, but they can also
rotate continuously.

Hawassa University, Institute of Technology School of


Electrical and Computer Engineering 16/04/2018 25

You might also like