0% found this document useful (0 votes)
12 views

Arduino Essential Training

The document describes the structure of an Arduino program. It explains that Arduino programs define variables, INPUT/OUTPUT pins, and the main program code. It also covers pin setup, pinMode syntax to set pins as inputs or outputs, digitalWrite syntax to set pin states, and delay to pause the program. It provides examples of blinking an LED, alternating LEDs, reading analog sensor values, and mapping values. It poses challenges to build circuits with a button and potentiometer and code the Arduino to control an LED.

Uploaded by

vvjain2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Arduino Essential Training

The document describes the structure of an Arduino program. It explains that Arduino programs define variables, INPUT/OUTPUT pins, and the main program code. It also covers pin setup, pinMode syntax to set pins as inputs or outputs, digitalWrite syntax to set pin states, and delay to pause the program. It provides examples of blinking an LED, alternating LEDs, reading analog sensor values, and mapping values. It poses challenges to build circuits with a button and potentiometer and code the Arduino to control an LED.

Uploaded by

vvjain2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Arduino Program Structure

Define variables here

Define INPUT/OUTPUT Pins here Sketch = Program =


Source Code

Create your main program here

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 68


Pin Setup

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 69


PinMode Syntax

pinMode(pin, INPUT/OUTPUT)

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 70


Digital Output

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 71


digitalWrite
Syntax:
digitalWrite(pin, HIGH/LOW)

Eg
digitalWrite(9,HIGH)

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 72


delay
Pauses the program for the amount of
time (in miliseconds) specified as
parameter

Syntax
delay(ms)

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 73


Connection

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 74


Exercise: pinMode, digitalWrite
1. Write a sketch to turn on one LED
Use digital pin 12 and use digitalWrite

2. Write a sketch to blink one LED

3. Write a sketch to alternate blinking 2 LEDs

Time for Exercise: 10 mins

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 75


Turn on LED

// the setup function runs once when you


press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an
output.
pinMode(12, OUTPUT);
}

// the loop function runs over and over again


forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on
(HIGH is the voltage level)
}

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 76


Blink LED
// the setup function runs once when you press
reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an
output.
pinMode(12, OUTPUT);
}

// the loop function runs over and over again


forever
void loop() {
digitalWrite(12, HIGH); // turn the LED on
(HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by
making the voltage LOW
delay(1000); // wait for a second
}

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 77


Two LED: Hint on Connection

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 78


Analog Output

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 79


Analog Output Applications

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 80


Pulse Width Modulation (PWM)
To create an analog signal, the microcontroller
use a technique called PWM. By varying the
pulse width or duty cycle, we can create an
analog voltage.

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 81


Analog Output
A few PINs on the Arduino Digital PINs allow to
modify the output to mimic analog signal

Pin 3, 5, 6, 9, 10, 11

They are indicated by a "~" besides the pin no.

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 82


Analog Write Pin

The analog write


pins are those pins
with ~ sign

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 83


analogWrite Syntax
Syntax
analogWrite(PIN, value)

Eg
analogWrite(9, 125)

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 84


Connection

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 85


LED analog value
void setup() {
// initialize digital pin LED_BUILTIN as an
output.
pinMode(9, OUTPUT);
}

// the loop function runs over and over again


forever
void loop() {
analogWrite(9, 0) ;
delay(1500); // wait for a second
analogWrite(9, 50) ;
delay(1500);
analogWrite(9, 150) ;
delay(1500);
analogWrite(9, 255);
delay(1500); // wait for a second
}

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 86


Exercise: analogWrite
1. Write a simple sketch to change the
brightness the LED light from low to high
repeatedly

2. Write a sketch to fade the LED brightness


from low to high, and high to low repeatedly.

Time: 10 mins

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 87


Analog Input

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 88


AnalogRead Syntax
Syntax
analogRead(PIN)

Eg
analogRead(A0)

• 10-bit analog to digital converter


• map input voltages between 0 and 5 volts into
integer values between 0 and 1023.
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 89
Exercise: analogRead
Write a simple sketch to read the analog output
of a potentiometer and output to the serial
monitor

Time: 10 mins

A0
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 90
Map
Syntax:
map(value, fromLow, fromHigh, toLow, toHigh)

Eg

int val = analogRead(0);


val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 91


Map

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 92


Map

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 93


Challenges

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 94


Challenge 1: Digital Read/Write
Parts Needed:
● Arduino Uno
● Solderless breadboard
● 1 LED
● 1 220 resistor
● 1 10K resistor
● 1 pushbutton
● Wires

10KΩ
● Connect the circuit shown on
the left
● Write a sketch from bare
minimum to turn on the LED
Time: 15mins when the button is pressed

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 95


Challenge 2: Analog Read/Write
Parts Needed:
● Arduino Uno
● Solderless breadboard
● 1 LED
● 1 220 resistor
● 1 Potentiometer
● Wires

● Connect the circuit shown


on the left
● Code a sketch to vary the
brightness of the LED
using potentiometer.

EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 96

You might also like