Embedded System Programming
Embedded System Programming
This section gives a clear introduction to Arduino (atmega family) microcontroller and it’s
Arduino is an open source programmable circuit board that can be integrated into a wide variety
of makerspace projects both simple and complex. This board contains a microcontroller which is
able to be programmed to sense and control objects in the physical world. By responding to
sensors and inputs, the Arduino is able to interact with a large array of outputs such as LEDs,
motors and displays. Because of its flexibility and low cost, Arduino has become a very popular
choice for makers and maker-spaces looking to create interactive hardware projects.
Arduino was introduced back in 2005 in Italy by Massimo Banzi as a way for non-engineers to
have access to a low cost, simple tool for creating hardware projects. Since the board is open-
source, it is released under a Creative Commons license which allows anyone to produce their
own board. If you search the web, you will find there are hundreds of Arduino compatible clones
and variations available but the only official boards have Arduino in it’s name.
In the next section, we’re going to discuss a few of the Arduino boards available and how they
having to choose the right board. If you’re brand new to this, you might have always thought that
there was just one “Arduino” board and that’s it. In reality, there are many variations of the
official Arduino boards and then there are hundreds more from competitors who offer clones.
Below are a few examples of the different types of Arduino boards out there. The boards with the
name Arduino on them are the official boards but there are also a lot of really great clones on the
market as well. One of the best reasons to buy a clone is the fact they are generally less
Another factor to consider when choosing a board is the type of project you are looking to do.
Arduino Uno
One of the most popular Arduino boards out there is the Arduino Uno. While it was not actually
the first board to be released, it remains to be the most actively used and most widely
Source Sparkfun.com
Board Breakdown
Here are the components that make up an Arduino board and what each of their functions are.
1. Reset Button – This will restart any code that is loaded to the Arduino board
2. AREF – Stands for “Analog Reference” and is used to set an external reference voltage
3. Ground Pin – There are a few ground pins on the Arduino and they all work the same
4. Digital Input/Output – Pins 0-13 can be used for digital input or output
5. PWM – The pins marked with the (~) symbol can simulate analog output
6. USB Connection – Used for powering up your Arduino and uploading sketches
8. ATmega Microcontroller – This is the brains and is where the programs are stored
9. Power LED Indicator – This LED lights up anytime the board is plugged in a power source
10. Voltage Regulator – This controls the amount of voltage going into the Arduino board
11. DC Power Barrel Jack – This is used for powering your Arduino with a power supply
12. 3.3V Pin – This pin supplies 3.3 volts of power to your projects
14. Ground Pins – There are a few ground pins on the Arduino and they all work the same
15. Analog Pins – These pins can read the signal from an analog sensor and convert it to digital
of ways. You can do what most people do and connect the board directly to your computer via a
USB cable. If you want your project to be mobile, consider using a 9V battery pack to give it
Arduino Breadboard
Another very important item when working with Arduino is a solder-less breadboard. This
device allows you to prototype your Arduino project without having to permanently solder the
circuit together. Using a breadboard allows you to create temporary prototypes and experiment
with different circuit designs. Inside the holes (tie points) of the plastic housing, are metal clips
On a side note, the breadboard is not powered on its own and needs power brought to it from the
Arduino board using jumper wires. These wires are also used to form the circuit by connecting
Here is a visual of what a completed Arduino circuit looks like when connected to a breadboard.
Source Sparkfun.com
On a side note, the breadboard is not powered on its own and needs power brought to it from the
Arduino board using jumper wires. These wires are also used to form the circuit by connecting
Once the circuit has been created on the breadboard, you’ll need to upload the program (known
as a sketch) to the Arduino. The sketch is a set of instructions that tells the board what functions
it needs to perform. An Arduino board can only hold and perform one sketch at a time. The
software used to create Arduino sketches is called the IDE which stands for Integrated
Development Environment.
Every Arduino sketch has two main parts to the program:
Void setup() – Sets things up that have to be done once and then don’t happen again.
Void loop() – Contains the instructions that get repeated over and over until the board is turned
off.
Selecting a Communication Port
Arduino Sensors
If you want your Arduino to sense the world around it, you will need to add a sensor. There are a
wide range of sensors to choose from and they each have a specific purpose. Below you will find
Ultrasonic Sensor
Light Sensor
Pressure Sensor
Proximity Sensor
Acceleration Sensor
Source Sparkfun.com
In digital pins, you have just two possible states, which are on or off. These can also be referred
For example, if an LED is on, then, its state is High or 1 or 5V. If it is off, you’ll have Low, or 0
or 0V.
In analog pins, you have unlimited possible states between 0 and 1023. This allows you to read
sensor values. For example, with a light sensor, if it is very dark, you’ll read 1023, if it is very
bright you’ll read 0 if there is a brightness between dark and very bright you’ll read a value
PWM pins are digital pins, so they output either 0 or 5V. However these pins can output “fake”
intermediate voltage values between 0 and 5V, because they can perform “Pulse Width
Modulation” (PWM). PWM allows to “simulate” varying levels of power by oscillating the
To control a digital output you use the digitalWrite() function and between brackets you write,
the pin you want to control, and then HIGH or LOW. To control a PWM pin you use the
analogWrite() function and between brackets you write the pin you want to control and a
Reading an input
To read an analog input you use the function analogRead() and for a digital input you use
digitalRead(). The best way for you to learn Arduino is practicing. So, choose a project and start
building something.
Blinking an LED
LEDs are small, powerful lights that are used in many different applications. It is as simple as
turning a light on and off. Establishing this important baseline will give you a solid foundation as
Components Required
1 × Breadboard
1 × Arduino Uno R3
1 × LED
1 × 330Ω Resistor
n × Jumper
To find out the polarity of an LED, look at it closely. The shorter of the two legs, towards the flat
edge of the bulb indicates the negative terminal. Components like resistors need to have their
terminals bent into 90° angles in order to fit the breadboard sockets properly. You can also cut
// the setup function runs once when you pressed reset or power the board
void setup() {
digitalWrite(ledpin, LOW);// turn the LED off by making the voltage LOW
pinMode(2, OUTPUT) − Before you can use one of Arduino’s pins, there is a need to tell
do this. digitalWrite(2, HIGH) − When you are using a pin as an OUTPUT, you can command
Turns on and off a light emitting diode (LED) connected to digital pin 13, when pressing a
Components Required
1 × Arduino Uno R3
1 × LED
1 × 330Ω Resistor
1 x Push Button
Schematic
Code
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
Traffic Lights
There are 3 LEDs with different colors (green, yellow and red) to mimic the traffic lights
There are 2 LEDs with different colors (green and red) to mimic the traffic lights for the
pedestrians.
Components required
1x Breadboard
Arduino UNO
5x 220Ohm Resistor
1x 10kOhm Resistor
1x pushbutton
Jumper Wires
Schematic
Code
The car light is always green, and so the pedestrian light is always red unless someone
The lights are in this state for a while (in the code this time is the variable crossTime).
All these actions will be inside the function changeLights(). Everytime you want to change the
Serial.begin(9600);
}
void loop() {
// this variable will tell us if the button is pressed
int state = digitalRead(button);
Serial.println(state);
// if the button is pressed and if it has passed 5 seconds since last button
press
if (state == HIGH && (millis() - changeTime) > 5000) {
//call the function to change the lights
changeLights();
}
}
void changeLights() {
digitalWrite(greenCar, LOW); // the green LED will turn off
digitalWrite(yellowCar, HIGH); // the yellow LED will turn on for 2 second
delay(2000);
digitalWrite(yellowCar, LOW); // the yellow LED will turn off
digitalWrite(redCar, HIGH); // the red LED will turn on for 5 seconds
digitalWrite(redPed, LOW);
digitalWrite(greenPed, HIGH);
delay(crossTime);
// flash the ped green
for (int x=0; x<10; x++) {
digitalWrite(greenPed, LOW);
delay(100);
digitalWrite(greenPed, HIGH);
delay(100);
}
digitalWrite(greenPed, LOW);
digitalWrite(redCar, LOW);
digitalWrite(redPed, HIGH);
digitalWrite(greenCar, HIGH);
changeTime = millis();
}
Seven-segments are practical, efficient, and cost-effective devices that are used to display
alphanumeric numbers. You will find many practical applications of these displays around you.
For example, if you go to the market, you will see a large digital clock based on a seven-segment
display. In movies, you have seen a hero trying to deactivate a bomb and at the time he keeps his
eye on the digital clock. If you notice the digital clock, it also uses 7-segment devices.
For example, if we are designing an Ac voltmeter or ac current meter using Arduino, we can use
a seven-segment display instead of a 16X2 liquid crystal display to display voltage and current
values. The seven-segment display has many applications in embedded systems and Arduino
based projects.
One of the popular examples for the use of a seven-segment display is the NASA space shuttle
launch countdown display.
There are two types of seven segment displays such as common anode and common cathode.
In common anode display, all the anodes terminals of eight light emitting diodes are common
and connect with 5 volt power supply. In normal condition, we apply logic high from Arduino to
each segment. Therefore, each segment remains off or does not glow. Similarly, when we want
to turn on a specific LED of a seven-segment device, we provide logic low signal. Because LED
glows only when there will be a logic high signal on anode side and logic low signal on cathode
side such is the case of common anode type display.
Components required
1x Breadboard
Arduino UNO
Schematic
Code
#include "SevSeg.h"
SevSeg sevseg;
void setup()
{
//define number of seven-segment digits to one
byte sevenSegments = 1;
//variable used to define the number of seven segment and common pins
//but we are using only one 7-segment. Hence leave it empty
byte CommonPins[] = {};
//array to store arduino pin connections with LED segments in order: A, B,
C, D, E, F, G, DP
byte LEDsegmentPins[] = {2, 3, 4, 6, 7, 8, 9, 5};
bool resistorsOnSegments = true;
//Initialize sevseg object that created above with input arguments defined
already
sevseg.begin(COMMON_ANODE, sevenSegments, CommonPins, LEDsegmentPins,
resistorsOnSegments);
//uncomment the following line for common cathode type display and comment
the above
//sevseg.begin(COMMON_CATHODE, sevenSegments, CommonPins, LEDsegmentPins,
resistorsOnSegments);
sevseg.setBrightness(90);
}
void loop()
{
//loop display counter values from 0-9
for(int i = 0; i < 10; i++)
{
sevseg.setNumber(i); // Display counter value
sevseg.refreshDisplay(); // refresh required
delay(1000); // delay of one second
}
}
available like 8x1, 8x2, 10x2, 16x1, 16x2 etc. All the above mentioned LCD display have 16
pins and the programming approach is also the same. The LCD can work in two different modes,
Schematic
Code
#include <LiquidCrystal.h>
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print("Hello, World!");
delay(1000);
lcd.clear();
lcd.print("Proteus vs Arduino");
delay(1000);
lcd.clear();
}
DC motor
Servo motor
Stepper motor
A DC motor (Direct Current motor) is the most common type of motor. DC motors normally
have just two leads, one positive and one negative. If you connect these two leads directly to a
battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite
direction.
Warning − Do not drive the motor directly from Arduino board pins. This may damage the
board. Use a driver Circuit or an IC (L298D).
Components Requirement
1x Arduino UNO
2x Small 5V DC Motor
1x L293D
Schematic
Code
int ena = 9;
int in1 = 8;
int in2 = 7;
int in3 = 2;
int in4 = 4;
void setup() {
// put your setup code here, to run once:
pinMode(ena, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
// for clockwise
analogWrite(ena, 180);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// for brake
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
delay(1000);
// for counter clockwise
analogWrite(ena, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(2000);
// for brake
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(1000);
}
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It
offers excellent non-contact range detection with high accuracy and stable readings in an easy-
to-use package. From 2cm to 400 cm or 1” to 13 feet. Its operation is not affected by sunlight or
black material
The ultrasonic sensor uses sonar to determine the distance to an object. Here’s what happens:
Arduino UNO
Breadboard
Jumper wires
Schematic
Code
Components required
Arduino UNO
1x LED
Jumper wires
Schematic
Code
The MQ-2 smoke sensor is sensitive to smoke and to the following flammable gases:
LPG
Butane
Propane
Methane
Alcohol
Hydrogen
The resistance of the sensor is different depending on the type of the gas.
The smoke sensor has a built-in potentiometer that allows you to adjust the sensor digital output
(D0) threshold. This threshold sets the value above which the digital pin will output a HIGH
signal.
The voltage that the sensor outputs changes accordingly to the smoke/gas level that exists in the
atmosphere. The sensor outputs a voltage that is proportional to the concentration of smoke/gas.
In other words, the relationship between voltage and gas concentration is as shown:
The greater the gas concentration, the greater the output voltage
The lower the gas concentration, the lower the output voltage
The output can be an analog signal (A0) that can be read with an analog input of the Arduino or a
digital output (D0) that can be read with a digital input of the Arduino.
In this example, sensor analog output voltage and when the smoke reaches a certain level is read,
it will make sound a buzzer and a red LED will turn on. When the output voltage is below that
Components required
Arduino UNO
1x Breadboard
1 x red LED
1 x green LED
1 x buzzer
3 x 220Ω resistor
Jumper wires
Schematic
Code