You are on page 1of 30

Microcontroller 2 :

The Basics
There are 3 main
categories of
components

MCU /
Sensor Microcontroller /
ESP32 Actuator
09/10/2023 2
09/10/2023
NOT THIS MCU! 3
Sensor Actuator
(Provides data to MCU) Microcontroller (Make an action)
(Compute and Make
decision)
Sensor (NON EXHAUSTIVE LIST)

• Ultrasonic
• Passive Infrared
• Temperature
• Humidity
• Light intensity
• PH meter

09/10/2023 5
Ultrasonic sensor
(Distance measurement)

09/10/2023 6
Passive Infrared
(PIR)
(Movement
monitoring)

09/10/2023 7
Temperature sensor
(Temperature measuring)
Ambient Light Sensor
(Light intensity)

09/10/2023 9
Sensor • They come in different shapes and sizes!
• Different shape, different use cases
• Example : Temperature sensor for measuring surface
temperature is different than for measuring
liquid/ambient temperature

09/10/2023 10
ALWAYS REFER
TO THE
DATASHEET!

They provide what voltage they operate on


What range they can measure
How to connect the pins

Professional Moron 11
Datasheet example

09/10/2023 12
Actuator (Non
Exhaustive List)

• LED
• Servo motor
• Wireless module

09/10/2023 13
Actuators
• They make action based on
decision made by the MCU
• They can either : Light up,
Move, Send data or anything
• Creativity is the key, think
outside the box!
• Don’t be afraid to try things,
they have safeguards.
• Worst case : you break a IDR
50.000 worth of electric
component but you get an
important lesson

09/10/2023 14
Simulate things
(Wokwi.com)

Worksheet

09/10/2023 15
Go to Wokwi.com • Create a new ESP32 Project
• Press the blue (+) button

09/10/2023 16
Parts! Finally!

You will find most of the parts


you need here, from resistor,
joystick, to biaxial stepper motor

09/10/2023 17
Simple thing first

Let’s try to add 5 LEDs, make it so all 5 LEDs


have different colors

09/10/2023 18
No Datasheet?
But how do I wire
it?

If you click the “?” icon there


will be reference for the
selected component

09/10/2023 19
Manage your
wirings! (Good
practice)

To manage your wirings, click on where you


want the wire connection to start, then click
where you want it to end. Then click on your
cable, you will see purple “Handle” to move
your wires around.

09/10/2023 20
Less overlapping wires = BETTER

Complete the rest of your


09/10/2023
wires 21
Powering up the LED

• GPIO pin can output HIGH or LOW


• The GPIO pins on ESP32 board will provide 3.3 V if it’s set to “HIGH” status
• The voltage is enough to drive a small LED, and its low current won’t burn the LED without resistor
• REMEMBER! Good practice is to check the datasheet of actual part. In this case we don’t need resistor
because ESP32 can only provide small current. BUT A GOOD ENGINEERING PRACTICE WOULD
PUT A RESISTOR BEFORE THE LED ANODE
• In this example, the LEDs
are wired to GPIO PINs :
• D23(Red LED)
• D22(Blue LED)
• D19(Green LED)
• D18(Light Blue LED)
• D5(Yellow LED)
• You don’t have to follow
exactly which PIN you’re
using, try the other GPIO
PINs!

09/10/2023 23
Now the fun part
begins

Programming

09/10/2023 24
#define <variable name> <pin number>
Setup() Defining PIN will make it easier down the road,
if you change the connected PIN of a component,
all you have to change is the define

09/10/2023 25
Setup() • Now, each of the PINs
connected to LED have to be
declared as OUTPUT
• If you are using 5 PINs to
power 5 LEDs then you have
to set all the pinModes for
all the PINs (this example
only shows 3 pinMode calls,
but you should make 5
pinMode calls)

09/10/2023 26
Loop()

• To output 3.3V from the GPIO PIN, use the


digitalWrite() function
• if set to HIGH the PIN will provide 3.3V
• If set to LOW the PIN will provide 0V
• This code will turn ON all the LEDs

09/10/2023 27
Now let’s modify it so the
LEDs are turning on in
sequence, then turn it all OFF

Adding a 2000ms delay for each digitalWrite() call


will make it so each LED has 2 second delay before
turning on

09/10/2023 28
Continued

Adding digitalWrite(<pin>, LOW) will turn off the


LEDs
Try the code and see what happens (Make sure you
are using corresponding PINs for EACH LEDs)

09/10/2023 29
Exercise

You might also like