Arduino
Arduino
Arduino
Course
Key Components
• Microcontroller: The brain of the board (e.g., ATmega328 on Arduino Uno).
• Digital Pins: Used for digital input/output (e.g., LEDs, buttons).
• Analog Pins: Used for analog input (e.g., sensors).
• Power Supply: Can be powered via USB or external power source.
• Communication Ports: Serial, I2C, SPI for interfacing with other devices.
ESP32 Hardware
ESP32 Overview
• A low-power Wi-Fi and Bluetooth microcontroller with various input/output capabilities.
• Developed by Espressif Systems, commonly used in IoT applications.
Key Components
• Microcontroller: Dual-core Tensilica Xtensa LX6 processor with up to 240 MHz clock speed.
• Digital Pins: Supports multiple GPIO pins for digital input/output operations.
• Analog Pins: Features ADC (Analog-to-Digital Converter) and DAC (Digital-to-Analog Converter).
• Power Supply: Operates at 3.3V, powered via USB or external Li-ion battery.
• Wireless Connectivity: Built-in Wi-Fi (802.11 b/g/n) and Bluetooth (Classic & BLE) for wireless
communication.
• Communication Ports: Supports UART, I2C, SPI, PWM, and CAN protocols for external device
interfacing.
Arduino Uno board
ESP-32 board
Arduino Software (use for Arduino UNO & ESP32)
Arduino IDE
• Integrated Development Environment for writing, compiling, and uploading code to Arduino
boards.
• Available for Windows, Mac, and Linux.
Key Features
• Code Editor: Write and edit Arduino sketches (programs).
• Compiler: Convert sketches into machine code that the microcontroller can execute.
• Uploader: Transfer the compiled code to the Arduino board via USB.
• Serial Monitor: Communicate with the board and debug sketches using serial communication.
Programming Language
• Based on C/C++ with simplified syntax and functions.
• Uses libraries to add functionality (e.g., controlling motors, reading sensors).
How to Program Arduino & ESP 32
Setup Function:
• Purpose: Initializes settings.
• Runs Once: This code runs once when
the Arduino is powered on or reset.
Loop Function:
• Purpose: Contains the main logic of the
program.
• Runs Repeatedly: This code runs in a
loop after the setup function has
completed.
Arduino IDE simple sketch.
Common Functions Used
in Arduino Programming
pinMode():
• Purpose: Configures a specified pin to
behave either as an input or an output.
digitalWrite():
• Purpose: Sets a specified digital pin to
HIGH or LOW.
digitalRead():
• Purpose: Reads the value from a
specified digital pin, either HIGH or
LOW.
Example:
Common Functions Used
in Arduino Programming
analogRead():
• Purpose: Reads the value from a
specified analog pin.
analogWrite():
• Purpose: Writes an analog value
(PWM wave) to a specified pin.
Example:
Common Functions Used
in Arduino Programming
delay():
• Purpose: Pauses the program
for a specified number of
milliseconds.
millis():
• Purpose: Returns the number of
milliseconds since the Arduino
board began running the current
program.
Common Functions Used
in Arduino Programming
Serial.begin():
• Purpose: Sets the data rate in bits per
second (baud) for serial data
transmission.
Serial.print():
• Purpose: Prints data to the serial port
as human-readable ASCII text.
Serial.println():
• Purpose: Prints data to the serial port
as human-readable ASCII text,
followed by a carriage return character.
Interrupts
Definition
• Interrupts are signals that temporarily halt
the main program execution to
immediately run a special function called
an Interrupt Service Routine (ISR).
Purpose
• To handle time-critical tasks, respond to
external events, or process real-time
signals without delay.
Benefits
• Allows for efficient handling of tasks
without continuously checking for events
within the main loop.
Using Interrupts in Arduino
Attaching an Interrupt:
• Use the attachInterrupt() function to specify which pin and condition will
trigger the interrupt, and which ISR to call.
• Parameters:
• pin: The pin number where the interrupt is attached.
• ISR: The name of the function to call when the interrupt occurs.
• mode: The condition that triggers the interrupt (LOW, CHANGE,
RISING, FALLING).
Example:
Sensors
Ultrasonic Sensor
Introduction:
• Definition: Ultrasonic sensors use sound
waves to measure the distance between the
sensor and an object.
• Common Model: HC-SR04.
Working Principle:
• Sound Waves: Sends out an ultrasonic
wave and measures the time it takes for the
echo to return.
• Distance Calculation: Uses the speed of
sound to calculate distance based on the
time it took for the echo to return.
Pin Configuration
Introduction:
• Definition: Infrared (IR) sensors use infrared
light to detect objects or measure the distance
between the sensor and an object.
• Common Model: TCRT5000.
Working Principle:
• Infrared Light: Emits an infrared light beam
and detects the reflection from an object.
• Distance Calculation: Measures the intensity
of the reflected light to determine the
presence or distance of an object.
Pin Configuration
L298N H-Bridge is a motor driver module that allows control of two DC motors. It supports
forward/reverse rotation, speed control via PWM.
How to connect:
Method 1:
Direction Control:
Two input pins (IN1, IN2) for each motor determine the motor's direction.
IN1= High , IN2 = Low → Motor rotates forward.
IN1= Low , IN2 = High → Motor rotates backward.
Speed Control:
Speed is adjusted using Pulse Width Modulation (PWM) applied to the Enable pin (EN).
Higher duty cycle → Faster speed.
Lower duty cycle → Slower speed
Method 2:
You need to create an Arduino system that controls four LEDs based on the value of a
potentiometer.
Requirements:Use a potentiometer to determine how many LEDs should be turned on.
The system should work as follows:
• If the potentiometer value is between 0 and 255, turn on 1 LED.
• If the potentiometer value is between 256 and 511, turn on 2 LEDs.
• If the potentiometer value is between 512 and 767, turn on 3 LEDs.
• If the potentiometer value is between 768 and 1023, turn on all 4 LEDs.
Example 8 => LED Reaction Game
Unsolved example
Design an Arduino-based reaction game that uses LEDs, a potentiometer, and a button.
The goal of the game is to test the player's reaction time.
Requirements:
• Use a potentiometer to set the difficulty level of the game.
• An LED should light up randomly after a delay.
• The player must press a button as quickly as possible when the LED lights up.
• The system should display the player's reaction time using LEDs.
• The game should reset automatically after displaying the reaction time.
Example 9 => Print Your Name
Example 9 => Print Your Name
Example 10 => Distance Measurement
Example 10 => Distance Measurement