Arduino Bootcamp

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

Introduction

to Arduino!
What is a Microcontroller?

● A microcontroller is a VLSI(Very Large Scale Integration)


integrated circuit
● Basically it's a tiny computer programmable to run 1 program
● It serves as a bridge between physical world and computer world
● It primarily has 4 functions: Input, Process, Output and
Communicate
Basic structure of a microcontroller:
● Central Processing Unit (CPU)
● Program Memory(ROM - Read only memory)
● Data Memory (RAM-Random access memory )
● Clock circuit
● Timers and counters
● Input and output ports
● Serial Communication interphase
● Interrupt mechanism
● ADC and DAC
● USB (Universal Serial Bus)
CPU
● A microcontroller brain is named as CPU as it is employed to
fetch data, decode it and at the end complete the assigned task
successfully.
● A CPU may be a simple 4 bits to complex 128 bits processor.
● The instruction fetched by memory is decoded in the CPU.

Memory
● Memory chip stores all the program and data
● Microcontrollers are built in with certain amount of RAM or ROM
Timers and Counters

● All microcontrollers have clocks within them s0 that programs


can be executed in rhythm with the clock.
● The timer and counter functions in the microcontroller simply
count in sync with the microcontroller clock.
Input and Output ports

● Inputs and Outputs ports are basically employed to interphase or


drive various appliances.
Serial Communication Interface
● Serial communication interface allows a microcontroller to be
connected to another microcontroller or to a pc using a serial
cable.

Interrupt mechanism
● Interrupts are basically mechanisms to make the CPU stop
processing one task and temporarily switch to another
● They are typically used for time critical applications where
immediate response to changing condition is required.
ADC and DAC

● ADC (Analog to Digital Converter) is employed to convert analog


signals into digital ones.
● DAC (Digital to Analog Converter) is employed to convert digital
signals into analog signals.
Applications of Microcontrollers

● Front Panel control in devices such as oven, washing machines,


refrigerators etc.
● Function generators
● Smoke and fire alarms
● Home automation systems
● Automatic Headlamps in cars
● Speed sensed door looking system
WHAT IS ARDUINO?
● Open-source Electronics prototyping platform
● Uses a “microprocessor”/ “microcontroller”
● Programmable in language similar to C
● Implements many inbuilt functions
● Multiple sensors available for use
● Lots of support online - very popular
PROGRAMMING ARDUINO
● Arduino uses its own IDE (Integrated Development Environment)
to write programs
● Head to https://www.arduino.cc/en/Main/Software and download
the software now!
STRUCTURE OF AN ARDUINO PROGRAM
void setup(){
…..
}

Void loop(){
…..
}
1 - USB CONNECTOR
2 - POWER JACK
3 - GROUND PINS
4 - 5V PINS
5 - 3.3V PINS
6 - ANALOG INPUT PINS
7/8 - DIGITAL PINS
Out of the 14 Pins, 6 can be used
for PWM (denoted by ~)
9- ANALOG REFERENCE//
10 - RESET BUTTON
11 - POWER LED
WHAT’S ON THE BOARD?

12 - TX/RX LEDS
13 - ATMEGA328P
14 - VOLTAGE
REGULATOR
Pins, On-board LEDs!!
DIGITAL PINS ● Digital Input/Output
pins(0,1 are serial in/out) are
numbered from 2-13

● PWM enabled
pins(3,5,6,9,10,11) can also be
used as Analog Outputs

● Most of the Analog Pins can


be used as Digital Pins.
● Numbered A0 to A5
ANALOG PINS
● Can be used for Analog
inputs/outputs(ADC etc)
with analogRead(),
analogWrite() and also
digitalWrite() as General
Purpose Input/Output pins.
Power Pins
● Vin is the input voltage to the board when it’s using an external
power source (as opposed to 5 volts from the USB connection or
other regulated power source from the power jack).
● You can supply voltage through this pin(from external circuitry),
or, if supplying voltage via the power jack, access it through this
pin.
● This is how you power the board when it’s not plugged into a USB
port for power. Can accept voltages between 7-12V(But the voltage
is regulated to a maximum of 5V)
LEDS
● Pin 13 LED: Useful for debugging, since the LED is in-built and
hence reliable in terms of connections.
● Power LED: Indicates that the board is receiving power. Useful
for debugging.
● TX and RX LEDs: Indicate communication(send/receive)
between the board and the computer. Flicker rapidly during
sketch upload as well as during serial communication. Useful
for debugging.
USB PORT
● Used for powering the board, and most importantly, uploading
your sketches to the board.
● Also used for communicating with the sketch (via Serial. println()
etc.).
BASIC FUNCTIONS
CONFIGURING THE PINS
pinMode(Pin_no , Mode):
Pin_no is the pin that is being set up. Pin_no can be either
specified as a number or even as a variable storing that
number.
Mode is either OUTPUT or INPUT.

Generally written in the ‘setup’ function


DIGITAL
digitalWrite(Pin_no, HIGH/LOW):
HIGH sets the pin to give a 5V (high) output while a low
sets the pin to give a 0V output.
Only to assign the value to the output pins.

digitalRead(Pin_no);
Gives the reading of the digital input pin as a high or a
low.
TIME
delay(T), delayMicroseconds(T):
Used to pause the program for certain amount of time.
(where T is in milliseconds & microseconds respectively)

micros(), millis():
Returns the time since the Arduino board began running
the current program.This value can overflow.
BLINKING LED
● The part before the setup() has the LED pin number and
delay initiated
● The single line in setup() specifies LED pin as an output pin
● In loop() digitalWrite makes the value of pin HIGH, delay
pauses execution for given time then pin is switched to
LOW and the delay function is called once more.
Time to Code!
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
// initialize digital pin LED_BUILTIN as an output.
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED
on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
by making the voltage LOW
delay(1000); //
wait for a second
}
ANALOG
analogWrite(Pin_no, Value):
Value (the duty cycle) is a 8 bit number (0-255) where 255 represents
5V and the intermediate value represents a corresponding voltage
<5V.
This is implemented by PWM. Only some pins are PWM enabled,
marked with ~ symbol before the pin no.

analogRead(Pin_no);
Reads the input from the analog pins marked as A0,A1...A5 on the
arduino board. This reading is a 10 bit value (0-1023).
Pulse Width Modulation(PWM)
Example Code!
int ledPin = 9; // LED connected to digital pin 9
void setup() { }
void loop() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
[Get Code]
Ultrasonic Sensor
How does an Ultrasonic sensor work?
● This sensor sends out pulses from T, which bounce off
from the object and R receives them.

● When the receiver gets the U.S. pulses it generates a


high signal. We get the time difference between the
pulses which is used to calculate the distance
How to calculate distance from the obstacle?
● The speed of sound is approximately 341 meters (1100 feet) per
second in air.

● Distance = (Time x Speed of Sound)/2


Useful function
pulseIn(Pin_no, HIGH):

Gives the time in milliseconds from the time this command is


initiated till Pin_no gives a HIGH pulse.
Sample
● Initialise variables before setup()
● Sets the data rate of data received from board using Serial.begin()
and sets trig pin to OUTPUT and echo pin to INPUT.
● First three lines of loop() send out a trigger pulse on the trig pin to
cause the sensor to emit ultrasound pulse.
● pulseIn(echo, HIGH) records the time after which the echo pin
receives a HIGH pulse.
● Data is converted into distance in cms and printed to the Serial
monitor.
Code! Measuring distance from an obstacle
const int trigPin = 9;

const int echoPin = 10;

// defines variables

long duration;

int distance;

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input


Serial.begin(9600); // Starts the serial communication

void loop() {

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in


microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

// Prints the distance on the Serial Monitor

Serial.print("Distance: ");

Serial.println(distance);

}
int trigPin =9;
int echoPin = 10;

void setup(){
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);

void loop(){
long duration, distance;
digitalWrite(trigPin,HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=(duration/2)/29.1;
Serial.println(distance);
}
https://create.arduino.cc/projecthub/will-su/touchless-automatic-motion-senso
r-trash-can-bbeed1?ref=tag&ref_id=ultrasonic&offset=2

https://create.arduino.cc/projecthub/straylight-burn-10/animated-pumpkin-a31
5ef?ref=tag&ref_id=ultrasonic&offset=11

https://create.arduino.cc/projecthub/walid-mafuj/android-motion-detector-ca
mera-with-arduino-mcu-306789?ref=tag&ref_id=ultrasonic&offset=7
INFRARED SENSOR
● I.R. sensor is used as a proximity sensor or collision sensor (not exact
distance).
● TX which emits IR light continuously.
● So, when an object is near the sensor light is reflected into the RX
which creates a voltage difference.
● The generation of this voltage difference depends on the intensity of
IR light received by the RX
● Note: You cannot use IR sensors outdoors or in bright lights
because the IR light of sun or from the bulb interacts with the RX.
Use US sensors instead.
void setup() {
pinMode(LED, OUTPUT);
pinMode(isObstaclePin, INPUT);
Serial.begin(9600);

}
void loop() {
isObstacle = digitalRead (isObstaclePin);
if (isObstacle == LOW)
{
Serial.println("OBSTACLE!!, OBSTACLE!!" );
digitalWrite (LED, HIGH);
Write for analog!
}
else
{
Serial.println("clear");
digitalWrite (LED, LOW);
}
delay(200);
}
void setup(){
pinMode(7,INPUT);
Serial.begin(9600);
pinMode(13,OUTPUT);
}

void loop(){

Serial.println(digitalRead(7));

if(digitalRead(7)==0)
{
digitalWrite(13,HIGH);
}
else{
digitalWrite(13,LOW);
}
}

You might also like