Embedded System Design
with
ESP32
Components
Name Image Type Function Notes
Push Button Digital Input Switch - Closes or Polarized, needs
opens circuit resistor
Trim Analog Input Variable resistor Also called a
Trimpot.
potentiometer
Photoresistor Analog Input Light Dependent Resistance varies
Resistor (LDR) with light.
Relay Digital Output Switch driven by a Used to control
small signal larger voltages
Temp Sensor Analog Input Temp Dependent
Resistor
Flex Sensor Analog Input Variable resistor
Soft Trimpot Analog Input Variable resistor Careful of shorts
RGB LED Dig & Analog 16,777,216 Ooh... So pretty.
Output different colors
Components
Components
ESP32
Go ahead and plug your board in!
LET’S CONNECT OUR Arduino
Take a copy of Arduino IDE.
([Link]
Connect your Arduino Uno via USB.
If you are using Windows,
follow the driver installation guide.
([Link]
[Link] Installarduino-drivers-on-windows-
8/)
4. Open the Arduino program
Arduino
Integrated Development Environment (IDE)
Two required
functions / methods
/ routines:
void setup()
{
// runs once
}
void loop()
{
error & status messages // repeats
}
Comments, Comments, Comments
Comments are for you – the programmer and your
friends…or anyone else human that might read your
code.
// this is for single line comments
// it’s good to put a description at the
top and before anything ‘tricky’
/* this is for multi-line comments
Like this…
And this….
*/
Settings: Tools Serial Port
Your computer
communicates to the
Arduino microcontroller
via a serial port
through a USB-Serial
adapter.
Check to make sure that
the drivers are properly
installed.
Settings: Tools Board
Next, double-check that the proper board is selected
under the ToolsBoard menu.
Libraries
The Arduino environment can be extended
through the use of libraries
Libraries provide extra functionality for use in
sketches.
Eg: GSM, LCD, GPS, RTC etc..
digitalWrite()
BIG 6 CONCEPTS
analogWrite()
digitalRead()
analogRead()
Loop statements / Boolean
Serial communication
Important commands to know…
commands are CASE-sensitive
pinMode(pin, INPUT/OUTPUT);
eg: pinMode(13, OUTPUT);
digitalWrite(pin, HIGH/LOW);
eg: digitalWrite(13, HIGH);
digitalRead(pin); // value is 1 or 0
eg: digitalRead(7);
analogWrite(pin,value);//pins 3, 5, 6, 9, 10, 11 ~
eg: analogWrite(3, 200);
//val – 8 bit value (0 – 255). 0 => 0V | 255 => 5V
analogRead(pin, value);
eg: analogRead(A0)
delay(time_ms);
eg: delay(1000); // delay of 1 sec.
Boolean Operators
<Boolean> Description
( ) == ( ) is equal?
( ) != ( ) is not equal?
( ) > ( ) greater than
( ) >= ( ) greater than or equal
( ) < ( ) less than
( ) <= ( ) less than or equal
1. LED Blinking (Digital output)
START
INITIALISE THE PINS &
PINMODES
TURN ON LED
BY PIN=HIGH
CALL DELAY
TURN OFF LED
BY PIN=LOW
CALL DELAY
A few simple challenges
Blink the same led with different
delay.
Blink 3 led at a time.
Toggle two led at a time.
Led flasher circuit.
Digital Input
• Connect digital input to your Arduino using Pins # 0 – 13
• Digital Input needs a pinMode command:
pinMode (pinNumber, INPUT);
• To get a digital reading:
int buttonState = digitalRead (pinNumber);
• Digital Input values are only HIGH (On) or LOW (Off)
Digital Sensors
• Digital sensors are more straight forward than
Analog
• No matter what the sensor there are only two
settings: On and Off
• Signal is always either HIGH (On) or LOW (Off)
• Voltage signal for HIGH will be a little less than 5V
on your Uno
• Voltage signal for LOW will be 0V on most systems
[Link] Button
START FLOW CHART
INITIALISE THE
PINS & PINMODES
NO
SWITCH TURN OFF LED
PRESSED BY PIN=LOW
YES
TURN ON LED
BY PIN=HIGH
Digital Sensors (Switches)
Pull-up Resistor (circuit)
to Digital Pin 2
int buttonPin = 2;
int ledPin = 13;
int buttonState ;
void setup() {
pinMode( ledPin, OUTPUT );
pinMode( buttonPin, INPUT );
}
void loop() {
buttonState = digitalRead (buttonPin);
if ( buttonState == HIGH )
digitalWrite( ledPin, HIGH );
else
digitalWrite( ledPin, LOW );
}
A few simple challenges
Control 2 led by using 2 pushbuttons.
Analog Sensors
Pin Analog Sensors = var. resistor
Take two sensors -- Use
the Serial Monitor and
find the range of input
values you get for each
sensor.
MaxAnalogRead = _________
MinAnalogRead = _________
Analog Sensors
Examples:
Sensors Variables
Mic soundVolume
Photoresistor lightLevel
Potentiometer dialPosition
Temp Sensor temperature
Flex Sensor bend
Accelerometer tilt/acceleration
[Link] METER
START FLOW CHART
INITIALISE THE
PINS & PINMODES
Read the TURN OFF LED
ANALOG INPUT BY PIN=LOW
CALL DEALAY
TURN ON LED
as function of
BY PIN=HIGH
ANALOG INPUT
[Link] LED-AnalogWrite
Note: The
longest leg of
the RGB LED is
the Common
Cathode. This
goes to GND.
Use pins 5, 6, &
9
How many unique colors can you
create?
Use [Link] or
experiment on your
own.
Pick out a few colors that
you want to try re-
creating for a lamp or
lighting display...
Play around with this
with the
analogWrite()
command.
int redPin = 5;
int greenPin = 6;
int bluePin = 9;
int i;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{ for(i=0;i<=255;i++)
analogWrite(redPin, i);
analogWrite (greenPin, i);
analogWrite (bluePin, i);
}
Project: Mood Lamp / Light
Sculpture
[Link] Serial Communication
Method used to transfer data between two devices.
Data passes between the computer and Arduino
through the USB cable. Data is transmitted as
zeros (‘0’) and ones (‘1’) sequentially.
Arduino dedicates Digital I/O pin # 0 to
receiving and Digital I/O pin #1 to
transmit.
Serial Monitor & analogRead()
Initializes the Serial
Communication
9600 baud data rate
prints data to serial bus
Additional Serial Communication
Sending a Message
void loop ( )
{
[Link](“Hands on “) ;
[Link](“Learning ”) ;
[Link](“is Fun!!!”) ;
}
Serial Communication:
Serial Debugging
void loop()
{
int xVar = 10;
[Link] ( “Variable xVar is “
) ;
[Link] ( xVar ) ;
}
Serial Communication:
Serial Troubleshooting
void loop ( )
{
[Link] (“Digital pin 9:
“);
[Link]
(digitalRead(9));
}
[Link]
START
FLOW CHART
Include LCD
Library function
INITIALISE LCD
PINS & PINMODES
Set cursor Display the content
Display the character
Set the cursor
/ string
#include<LiquidCrystal.h> // LCD library function
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // sets the interfacing pins
void setup()
[Link](16, 2); // initializes the 16x2 LCD
void loop()
[Link](0,0); //sets the cursor at row 0 column 0
[Link]("16x2 LCD MODULE"); // prints 16x2 LCD MODULE
[Link](2,1); //sets the cursor at row 1 column 2
[Link]("HELLO WORLD"); // prints HELLO WORLD
}
Automatic street Light
FLOW CHART
START
Turn OFF Relay
INITIALISE LCD
PINS & PINMODES
Turn ON Relay
Read the ANALOG
INPUT from LDR
YES
Intensity
< NO
Set a THRESHOLD
range for the Light threshol
Intensity d
int x= A0; // select the input pin for the potentiometer
int y = 13; // select the pin for the LED
int light_value = 0; // variable to store the value coming from the sensor
int converted value=0;
void setup()
{
pinMode( y, OUTPUT );
}
void loop()
{
light_value = analogRead (x);
converted value= 100 – light_value/10.24;
if( converted value >=90 ) // Turn OFF the light when light is 90 percent
digitalWrite(y, LOW);
Revers mode distance indicator
FLOW CHART
START
Turn on led2
Turn off led1
INITIALISE LCD PINS and display safe
& PINMODES
Turn on led1
Turn on Buzzer
and display STOP
Read the ANALOG
INPUT from IR sensor
YES
distance NO
Set 3 THRESHOLD <
ranges for the required threshold
safe distance range.
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int analogInPin = A1;
int x = 0;
int a= 13;
int b= 8;
int c= 9;
void setup()
{
[Link](9600);
[Link](16, 2);
[Link]();
[Link](" Reverse Mode ON ");
}
void loop()
{
x= analogRead(A1);
[Link](x);
delay(500);
if (x>=200)
{
[Link]();
[Link](0, 0);
[Link]("distance= ");
[Link] (x);
[Link](0, 1);
[Link] (" SAFE ");
digitalWrite(a,1);
digitalWrite(b,0);
digitalWrite(c,0);
}
else if(x<200 && x>25)
{
[Link]();
[Link](0, 0);
[Link]("distance= ");
[Link] (x);
[Link](0, 1);
[Link] (" SAFELESS ");
digitalWrite(a,0);
digitalWrite(b,1);
digitalWrite(c,0);}
else
{
[Link]();
[Link](0, 0);
[Link]("distance= ");
[Link] (x);
[Link](0, 1);
[Link] (" STOP ");
digitalWrite(a,0);digitalWrite(b,0);digitalWrite(c,1);
}
}
Robotics
The robot control loop
• Speech
• Vision • Task planning
• Temperature • Plan Classification
• Acceleration • Learn
• Position • Process data
• Distance • Path planning
• Touch • Motion planning
• Force
Sense Think
• Magnetic field
• Sound
• Light
Act
Output information Move,
Speech
Text, Visuals Wheels Legs
Arms Tracks
H-Bridge
L293D
Differential Drive(basics)
Line following Robot
A Line Follower Robot uses a series of sensors (whether it
is light reflection) to detect a line drawn on a surface.
This particular Line Follower uses two sensors that send
two separate signals to the Arduino Microcontroller
Arduino reads these signals and computes what to do
next.
The code depends on the user, whether to tell the
Arduino to
slow down one motor or
speed up the motor, or
simultaneously slow down one and speed up the other.
Questions?