Arduino: A Brief Introduction To Modern Micro-Controllers
Arduino: A Brief Introduction To Modern Micro-Controllers
RAMAN LAB 1
Micro-Controllers
• History:
• 1969: Four phase systems AL1.
• 1971: Intel 4004
• 2022: MARVELL 88MZ300 (SoC)
RAMAN LAB 2
Why Arduino ?
• Microcontrollers are notorious for being difficult to program
• The goal of Arduino is to create an accessible way for software developers to enter
the world of microcontroller programming
• Arduino is open source, both in its software and hardware
• Unlike most microcontroller interfaces, Arduino is cross-platform, so it can be run
on Windows, Linux, and macOS
• Arduino can interact with other software on the computer like Flash or even web
APIs
RAMAN LAB 3
Types of Arduino Boards
Arduino Nano
Arduino LilyPad Arduino Mega
RESET
SCL\SDA
(I2C Bus)
POWER
5V / 3.3V /
GND Digital I\O
PWM(3, 5, 6, 9,
Analog 10, 11)
INPUTS
Arduino UNO
RAMAN LAB 5
Layout of
Arduino
UNO
RAMAN LAB 6
Specifications
• Atmega328 Microcontroller:
• 32KB Flash Memory.
• 8 Bit Resolution.
• Tech Specs:
• USB Connector: USB B
• DI/DO Pins: 14
• AI Pins: 6
• PWM Pins: 6
• Communications:
• UART
• SPI
• I2C
• I/O Voltage: 5V
• Rated Supply Voltage: 7-12V
• Clock Speed:
• Main Processor: ATmega328P 16 MHz
• USB Serial Processor: ATmega16U2 16
MHz
• Memory: Atmega328p
• 2KB SRAM, 32KB FLASH, 1KB EEPROM
RAMAN LAB 7
Components
Name Image Type Function
Push Button Digital Input Switch - Closes
or opens circuit
Trim Analog Input Variable resistor
potentiometer
Photoresistor Analog Input Light Dependent
Resistor (LDR)
Relay Digital Output Switch driven by
a small signal
Temp Sensor Analog Input Temp Dependent
Resistor
Flex Sensor Analog Input Variable resistor
arduino.cc/en/main/software
RAMAN LAB 12
Connect Arduino to your
Computer
RAMAN LAB 13
Install Arduino Drivers
RAMAN LAB 14
Open Arduino IDE
VERIFY SERIAL
MONITOR
UPLOAD
NEW TAB
OPEN CODE GOES
SAVE HERE
BOARD &
SERIAL
PORT
SELECTIONS
RAMAN LAB 15
Settings: Tools Board
RAMAN LAB 16
Settings: Tools Serial Port
•Your computer communicates
to the Arduino microcontroller
via a serial port through a
USB-Serial adapter.
RAMAN LAB 17
Basic Coding
Notebook
RAMAN LAB 18
Data Types:
Type Sign Byte Bits Range Other Info.
s Min Max
char signed 1 8 -128 127 ASCII
char unsigned 1 8 0 255 ASCII
byte 1 8 0 255
int(Uno+) signed 2 16 -32768 32767 Uno model +others
short 2 16 -32768 32767
int(Uno+) unsigned 2 16 0 65535 Uno model +others
word 2 16 0 65535 Same as unsigned int
long signed 4 32 -2147483648 2147483647 Append with ‘L’
Long unsigned 4 32 0 4294967295
float 4 32 -3.4028235E+38 3.4028235E+38 6-7 dec digits of
precision
double (Uno+) 4 32 -3.4028235E+38 3.4028235E+38 Same as float
RAMAN LAB 19
Commonly used operators:
• Sum: + • AND: &&
• Product: * • OR: ||
• Division: / • NOT: !
• Subtraction: - • Equal to: ==
• Modulo: % • Greater than: >
• Exponent: ^ • Less than: <
• Greater than equal to: >=
• Less than equal to: <=
• Not Equal to: !=
RAMAN LAB 20
Basic Coding
setup() function
• Called when a sketch starts.
• To initialize variables, pin modes, start using
libraries, etc.
• Will only run once, after each power-up or reset of
the Arduino board.
loop() function
• Loops consecutively.
• Code in the loop() section of the sketch is used to
actively control the Arduino board.
Commenting
• Any line that starts with two slashes (//) will not be
read by the compiler, so you can write anything
you want after it. RAMAN LAB 21
pinMode()
• Instruction used to set the mode (INPUT or
OUTPUT) in which we are going to use a pin.
• E.g.: pinMode (13, OUTPUT);
• i.e. setting pin13 as output.
digitalWrite()
• Write a HIGH or a LOW value to a digital pin.
• E.g.: digitalWrite (11, HIGH);
• i.e. setting pin 11 to high.
RAMAN LAB 22
digitalRead()
• Reads the value from a specified digital pin,
either HIGH or LOW
• E.g.: int inPin=7;
val = digitalRead(inPin);
• ie. reads the value from inPin and assigns it to val.
delay()
• Pauses the program for the amount of time (in
milliseconds) specified as parameter.
• E.g.: delay(1000);
• ie. waits for a second (1000 ms = 1 s)
RAMAN LAB 23
Setup
The setup section is used for assigning input and outputs
(Examples: motors, LED’s, sensors etc) to ports on the Arduino
To do this we use the command “pinMode”
void setup() {
port #
pinMode(9, OUTPUT);
Input or Output
}
RAMAN LAB 24
Loop
void loop() { Port # from setup
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
Turn the LED on
or off
} Wait for 1 second
or 1000 milliseconds
RAMAN LAB 25
RAMAN LAB 26
TASK 1
• Using 3 LED’s (red, yellow and green) build a traffic light that
• Illuminates the green LED for 5 seconds
• Illuminates the yellow LED for 2 seconds
• Illuminates the red LED for 5 seconds
• repeats the sequence
• Note that after each illumination period the LED is turned off!
TASK 2
• Modify Task 1 to have an advanced green (blinking green LED) for 3
seconds before illuminating the green LED for 5 seconds
RAMAN LAB 27
Control Structure & Loop
If conditioning: Switch case: while(expression)
{ Block of statements; }
if(condition) switch(var)
{ {
Statement-1 Case 1:
…. // do something when var equal to
do { Block of statements; }
Statement-N 1
while (expression);
break;
}
else if(condition) Case 2:
{ // do something when var equal to for ( initialize; control; increment or decrement)
Statement 2 { // statement block }
} break;
else{Statement}
default:
//if nothing else matches, do the
default
//default is optional
RAMAN LAB 28
}
Practical Hands On
RAMAN LAB 29
Circuit #1:
Blinking an LED
LEDs (light emitting
diodes) are small,
powerful lights used in
many different
applications.
RAMAN LAB 30
Resistor Color Code
RAMAN LAB 31
Circuit Schematic
Pin 13 is a digital pin and can be used as an
output or an input. Here we will use it to
output power to the LED.
RAMAN LAB 33
RAMAN LAB 34
Create the Sketch
/*
Blink
Turns on an LED for one second then off for one second, repeatedly.
*/
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
} RAMAN LAB 35
Circuit #2: Potentiometer
How to read analog input from the physical world
using a potentiometer (“pot” for short) and control
the blink rate of an LED. We’ll also learn how to use
the serial monitor to watch how the voltage changes.
RAMAN LAB 36
Circuit Schematic
We’re adding a
potentiometer to
control the blink rate
of the LED.
RAMAN LAB 37
Design it!
RAMAN LAB 38
RAMAN LAB 39
Create the Sketch
int sensorPin =0;
int ledPin =13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int sensorValue;
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
Serial.println(sensorValue);
} RAMAN LAB 40
Circuit #3
Photo Resistor (Light Sensor)
• Photoresistors change resistance based on
how much light the sensor receives.
• Use our photo resistor in a “voltage
divider” configuration. Output:
• High voltage = lot of light
• Low voltage = little light
RAMAN LAB 41
Resistive Sensors & Voltage Dividers
Resistor in disguise!
• Arduino measures voltage, not
resistance. Need a voltage divider:
• Consists of two resistors.
Top resistor
• The “top” resistor is the sensor.
• The “bottom” resistor is a
normal, fixed resistor (usually 10
KΩ). Bottom resistor
RAMAN LAB 42
Pulse Width Modulation (PWM)
• Arduino is so fast it can blink a pin on and off 1,000 times per second.
• PWM pins also vary amount of time blinking pin spends on HIGH vs.
LOW.
RAMAN LAB 43
Pulse Width Modulation (PWM)
RAMAN LAB 44
Circuit Schematic
The left side of the schematic is
almost the same as the previous
circuits. What’s changed?
RAMAN LAB 45
Design it!
RAMAN LAB 46
RAMAN LAB 47
Create the Sketch
const int sensorPin=0;
const int ledPin=9;
int lightLevel, high=0, low=1023;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
lightLevel = analogRead(sensorPin);
autoTune();
analogWrite(ledPin, lightLevel);
Serial.println(lightLevel);
} RAMAN LAB 48
Create the Sketch
void autoTune()
{
if(lightLevel<low)
{
low=lightLevel;
}
if(lightLevel>high)
{
high=lightLevel;
}
lightLevel=map(lightLevel, low+30, high-30, 0, 255);
lightLevel=constrain(lightLevel, 0, 255);
} RAMAN LAB 49
Circuit #4:
Temperature Sensor
• Temperature sensors are used to measure
ambient temperature.
RAMAN LAB 50
Circuit Schematic
RAMAN LAB 51
Design it!
RAMAN LAB 52
RAMAN LAB 53
Create the Sketch
const int temperaturePin = 0;
void setup() {
Serial.begin(9600); float getVoltage(int pin)
}void loop() { {
float voltage, degreesC, degreesF;
return(analogRead(pin) *
voltage = getVoltage(temperaturePin);
0.004882814);
degreesC = (voltage - 0.5) * 100.0;
degreesF = degreesC * (9.0/5.0) + }
32.0;
Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.print(degreesC);
Serial.print(" deg F: ");
Serial.println(degreesF);
RAMAN LAB 54
delay(1000);}
RAMAN LAB 55