Professional Documents
Culture Documents
ARDUINO UNO
INTRODUCTION TO ARDUINO UNO
Features
An open-source microcontroller board
Microcontroller : ATmega328
Operating Voltage : 5V
Input Voltage (external) : 6-20V (7-12 recommended)
Digital I/O Pins : 14 (6 PWM output)
Analog Input Pins :6
DC Current per I/O Pin : 40 mA
DC Current for 3.3V Pin : 50 mA
Flash Memory : 32 KB (0.5 KB for bootloader)
Clock Frequency : 16 MHz
Programming Interface : USB (ICSP)
MODULE - 3 ECE4003 – EMBEDDED SYSTEM DESIGN
INTRODUCTION TO ARDUINO UNO
Arduino Uno - Board Details
Status
Register
CPU Device
Mechanism
Data
Register
Busy-Wait I/O
Checking an I/O device if it has completed the task by reading its status register
often called polling.
Example: AGAIN: JNB TF0, AGAIN
Interrupts
It enables I/O devices to signal the completion or status and force execution of a
particular piece of code.
Example: MOV IE, #82H
The list of possible analog input signals is endless, (e.g. audio and
video, medical or climatic variables)
ADC conversion D=
Where,
Vi is the input voltage
Vr the reference voltage
n the number of bits in the converter output
D the digital output value.
In Arduino board, analog pins have an ‘A’ in front of their label (A0
through A5).
434 =
= 2.12 V
Analog I/O
AREF pin and analogReference(type) ADC reference other than 5 V.
Increase the resolution available to analog inputs
That operate at some other range of lower voltages below +5
Must be declared before analogRead()
analogReference(type)
Parameters type:
which type of reference to use (DEFAULT,
INTERNAL, INTERNAL1V1, INTERNAL2V56, or
EXTERNAL)
Returns
None
MODULE - 3 ECE4003 – EMBEDDED SYSTEM DESIGN
ANALOG TO DIGITAL (ADC) CONVERTER
Analog I/O
analogRead(pin)
Reads the value from a specified analog pin with a 10-bit resolution.
Works for analog pins (0–5).
It will return a value between 0 to 1023.
analogRead(pin)
100 microseconds for one reading.
Reading rate 10000 per sec. Parameters pin:
the number of the analog input pin to read
INPUT nor OUTPUT need not be declared. from (0-5)
Returns
int(0 to 1023)
MODULE - 3 ECE4003 – EMBEDDED SYSTEM DESIGN
ANALOG TO DIGITAL (ADC) CONVERTER
analogWrite(pin,value)
Analog I/O
Parameters pin: the number of the pin you
analogWrite(pin,value) want to write
value: the duty cycle between 0 (always off,
Write an analog output on a digital pin. 0%) and 255 (always on, 100%)
EXAMPLE 1
Program an Arduino board to read analog value from an analogy input pin,
convert the value into digital value and display it in a serial window.
void setup()
{
//monitor output in serial port
Serial.begin(9600); //intitating serial communication with 9600 baud rate
}
void loop()
{
sensorValue = analogRead(A0);
//analog input is read from A0 pin
Serial.println(sensorValue);
//printing the output in serial port
delay(100); // simple delay of 100ms
}
CODE
EXAMPLE 2
void loop() {
int temp_adc_val;
float temp_val;
temp_adc_val = analogRead(lm35_pin); /* Read Temperature */
temp_val = (temp_adc_val * 4.88); /* Convert adc value to equivalent voltage */
temp_val = (temp_val/10); /* LM35 gives output of 10mv/°C */
if (temp_val > 40) {
digitalWrite(ledPin, HIGH); // turn LED on:
} else {
digitalWrite(ledPin, LOW); // turn LED off:
}
Serial.print("Temperature = ");
Serial.print(temp_val);
Serial.print(" Degree Celsius\n");
delay(1000);
}
CODE
+
𝑫 DAC =
-
Control Lines
MODULE - 3 ECE4003 – EMBEDDED SYSTEM DESIGN
DIGITAL TO ANALOG (DAC) CONVERTER
void loop()
{
potvalue=analogRead(A0)/4;
analogWrite (6, potvalue);
}
Control Registers
Timer/Counter Modes
Returns
None
MODULE - 3 ECE4003 – EMBEDDED SYSTEM DESIGN
TIMER / COUNTER
Parameters
us: the number of microseconds to pause
(unsigned int)
Returns
None
MODULE - 3 ECE4003 – EMBEDDED SYSTEM DESIGN
TIMER / COUNTER
Parameters
None
Returns
Number of milliseconds since the program
started (unsigned long)
MODULE - 3 ECE4003 – EMBEDDED SYSTEM DESIGN
TIMER / COUNTER
micros()
Parameters
None
Returns
Number of microseconds since the program
started (unsigned long)
MODULE - 3 ECE4003 – EMBEDDED SYSTEM DESIGN
TIMER / COUNTER
EXAMPLE
Digital I/O
Read the state of an input pin
ATmega 328P - Three 8-bit i/o ports and one 7-bit i/o port
PROGRAMMING LED & SWITCHES
Digital I/O
Input:
Read voltage applied to them
Change state due to electronic interference like static discharges (I/P pin
with no connection)
Pull-down resistor (going to ground) all ways connect unused I/P pin to
ground
PROGRAMMING LED & SWITCHES
Digital I/O
Output:
Up to 40 mA current can be delivered to output circuits,.
Pin Configuration
Inform the controller how it should operate
In Arduino based embedded system - pinMode(pin,mode)
Pin – number or variable (e.g. 1 to 13 or A0 to A5)
Digital pins are input by default pinMode(pin,mode)
Parameters
pin: the number of the pin whose mode you
wish to set
mode: INPUT, OUTPUT, or INPUT_PULLUP
Returns
None
PROGRAMMING LED & SWITCHES
digitalWrite(pin,value)
Pin Configuration
Parameters
digitalWrite(pin, value) Pin: the number of the pin you want to write
value: HIGH or LOW
Output pin
Write on or off Returns
None
digitalRead(pin,value)
digitalRead(pin, value)
Parameters
Input pin Pin: the number of the pin you want to read (int)
Read a state of a pin Returns
HIGH or LOW
PROGRAMMING LED & SWITCHES
EXAMPLE
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED on:
} else {
digitalWrite(ledPin, LOW); // turn LED off:
}
}
PROGRAMMING LED & SWITCHES
INTRODUCTION TO SERIAL PORT
Serial communication protocol is used by computers and electronic modules
to send and receive control information and data.
Arduino boards communicate with the PC via USB cable(using inbuilt USB
to TTL converter chip) or to other serial devices using the serial pins.
Arduino Uno has one serial port at digital pins 0 (Tx) and 1(Rx).
Arduino Uno - Board Details
void loop()
{
ss= digitalRead(12);
if(ss == HIGH){
Serial.println("SHRUDHI");
delay(1000); //wait for 1000 milliseconds
}
else if(ss == LOW){
Serial.println("19BLC1166");
delay(1000); //wait for 1000 milliseconds
}
}
TASK-3
Simulate and verify this logic on Arduino Uno using Tinkercad circuits
simulator.
TASK-3
Progra Output Design
m
INTRODUCTION TO ULTRASONIC SENSOR
An ultrasonic sensor is an electronic device that measures the distance of a
target object by emitting ultrasonic sound waves, and converts the reflected
sound into an electrical signal.
Ultrasonic sensors have two main components:
Transmitter (which emits the sound using piezoelectric crystals)
Receiver (which encounters the sound after it has travelled to and from the target).
In order to calculate the distance between the sensor and the object, the
sensor measures the time it takes between the emission of the sound by the
transmitter to its contact with the receiver.
The formula for this calculation is D = ½ T x C (where D is the distance, T is
the time, and C is the speed of sound ~ 343 meters/second).
DAY-5 Arduino Programming Workshop 68
INTRODUCTION TO ULTRASONIC SENSOR
Ultrasonic sensors are used primarily as proximity sensors.
They can be found in automobile self-parking technology, anti-collision
safety systems, robotic obstacle detection systems also used as level sensors
to detect, monitor, and regulate liquid levels in closed containers.
The HC-SR04 ultrasonic sensor offers excellent non-contact range detection
with high accuracy and stable readings from 2 cm to 400 cm.
It comes complete with ultrasonic transmitter and receiver module.
The HC-SR04 Ultrasonic Module has 4 pins, Ground (Gnd), VCC (5V) , Trig
and Echo. The trig and echo pins to any Digital I/O pin on the Arduino Board.
Then the HC-SR04 automatically sends Eight 40 kHz sound wave and wait for rising
edge at Echo pin.
When the rising edge capture occurs at Echo pin, start the Timer and wait for falling
edge on Echo pin.
As soon as the falling edge is captured at the Echo pin, read the count of the Timer.
This time count is the time required by the sensor to detect an object and return back
from an object (to and from). Distance = (Speed x Time)/ 2
Distance = (0.034 cm/µs x 500 µs) / 2
Distance = 8.5 cm
DAY-5 Arduino Programming Workshop 70
INTRODUCTION TO ULTRASONIC SENSOR
Distance = (Speed x Time)/ 2
Distance = (0.034 cm/µs x 500 µs) / 2
Distance = 8.5 cm
When no obstacles then timeout after 38ms When obstacle detected pulse width of 50 µS to 25 mS received
DAY-5 Arduino Programming Workshop 71
LAB TASK-1
Read the distance value from HC-SR04 and print it on serial monitor
Write a program to read distance value from HC-SR04 ultrasonic sensor module in
cm and print it on the serial monitor. Simulate and verify this logic on Arduino Uno
using Tinkercad circuits simulator.
pulseIn() - Reads a pulse (either HIGH or LOW) on a pin. For example, if value is
HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits
for the pin to go LOW and stops timing. Returns the length of the pulse in
microseconds or gives up and returns 0 if no complete pulse was received within the
timeout.
DAY-5 Arduino Programming Workshop 72
int trig = 11;
int echo = 10;
float timeduration;
float distance;
void setup()
{
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
digitalWrite(trig, LOW);
•
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
timeduration = pulseIn(echo, HIGH);
distance = 0.034 * timeduration / 2;
Serial.print("Distance in cm: ");
Serial.println(distance);
delay(500);
}
LAB TASK-1
Program & output Design