ARDUINO
EDUCATION
Basic Concepts
• Variable Declaration
I • Name
• Set Up
II • Uses command “void setup ( )”
• Instructions
III • Inserted between “void loop ( )” command
Used to store data.
int = integer (e.g. 1, 2, 3)
float = floating point (1.2, 2.33, 4.567)
Variables! boolean = true or false ( e.g. Do u love Arduino?)
char = a single character (e.g. a, b, c)
string = used to store more characters (e.g. names, words)
Setup Versus Loop
void setup ( ) void loop ( )
• Commands in this bracket will run • Commands in this bracket will run
once at startup. again and again.
INPUT OUTPUT
Sensors, LED,
Push buttons, RGB LED
Potentiometers Motor
Pin Mode • For input setup,
“ pinMode(port number/ name, INPUT); "
• For output setup,
“ pinMode(port number/ name, OUTPUT); "
Digital Versus Analog
Digital Analog
Digital input may provide Analog input may provide
if the water is hot or not. how hot the water is or how cold the water is.
Digital pins can be used to write HIGH or Pulse Width Modulation (or PWM) pins are
LOW. used to write analog signals.
Its output is either maximum voltage or They can be used to write a voltage level
minimum voltage. anywhere between the maximum and
minimum voltage.
What is PWM?
• Imagine u have a ceiling fan.
• If u turn it on, it will spin at full speed.
• Now imagine what will happen
- if u repeat turning it on and off for 2 seconds.
- or on for 3 seconds and off for 1 second.
• PWM is similar. But it happens in milliseconds.
Digital Read/ Write
Input Output
Digitally Read Digitally Write
digitalRead (pinNumber/ name); digitalWrite (pinNumber/ name , State);
State may be (HIGH/ LOW) , (1/0)
Eg: digitalRead (2); Eg : digitalWrite (9 , HIGH);
Digital Output
1 2 3 4
Open Arduino Go to File > Upload the LED on the
IDE. Example > program. board will start
Basics > Blink. blinking.
1. Write "pinMode(5, INPUT);" in setup bracket.
2. Now Arduino is ready to read data from pin
Digital
number 5.
Input 3. But we need to know if it is on or off.
4. Write "pinMode(LED_BUILTIN, OUTPUT);"
in setup bracket.
5. Now LED on arduino is ready to light up.
Digital Output
Take
• Breadboard
• Arduino Board
• USB Cable
• 1 x 220 Ohm Resistor
• 1 x LED
• 4 x jumper wire
Set it up as shown in Figure!
6. Write
if (digitalRead(5) == HIGH) {
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
in loop bracket.
7. Upload the code.
Pull up / Pull
down
• For digital input, you need to set
up pull down or pull up resistor.
• They are used to make digital
pins stable.
Circuit Design with Push Buttons (Pull DOWN)
Circuit Design with Push Buttons (Pull UP)
HOW
MACHINES
COUNT
Analog Read/ Write
Input Output
Analog Read Analog Write
analogRead (pinNumber/ name); analogWrite (pinNumber/ name , State);
Input State is between 0 ~ 1023. Output State is between 0 ~ 255.
Eg: analogRead (2); Eg : analogWrite (9 , State);
• Let's alter led fade with a potentiometer.
Analog 1. Write "pinMode(A0, INPUT);" in setup
Input 2.
bracket.
Now Arduino is ready to read data from
analog pin number A0.
Potentiometer Analog Output
Take
• Breadboard
• Arduino Board
• USB Cable
• 1 x 220 Ohm Resistor
• 10k Ohm Resistor
• 1 x LED
Ground • 1 x Potentiometer
Supply
• 7 x jumper wire
Set it up as shown in Figure!
Circuit Design with Potentiometer
Let's talk to Arduino!
• We can receive data and send data using serial commuincation.
• To use serial commuincation, we have to use
• Serial.begin, Serial.println and Serial.read commands.
• First we have to set up the speed(Baud Rate) between communication. Write "Serial.begin(9600);"
in setup bracket.
• Then, write "Serial.println("Mingalarbar");"
• Upload the code and open Serial Monitor.
Let's talk to Arduino!
Serial Port
The way the boards “talk” to the computer is through something called
a serial port.
Serial ports can be used to exchange fairly complicated data with the
computer.
Instead of the digital or analog signals, you can send or receive text.
The serial port uses the digital pins 0 and 1:
Pin 0: RX or receiver. Receives data to the board.
Pin 1: TX or transmitter. Sends data from the board.
digitalRead ( ) and digitalWrite ( ) on these pins cannot be used while using
serial communication.
The sender and receiver are communicating in the
same speed.
The speed is called baud rate and measures bits per
second.
The most common speed is 9600 bits per second.
Sending to computer
To send a message from the control board to the computer,
“ Serial.begin ( ) and Serial.println ( ) or Serial.print ( )”
commands are used.
Serial.begin( speed ): Initializes serial communication.
The baud rate, or bits per second, is set with speed.
Serial.println( "message" ): Prints message to the serial port.
The next message printed will start on a new line.
Serial.print( "message" ): Prints message to the serial port.
The next message will start right after the previous one, on the same line.
Set Up
1. Set up board and computer.
2. Open the serial monitor by clicking this icon
in the IDE.
Instructions
In setup( ), the serial communication is initialized with
the speed 9600 bits per second.
In loop( ), the message “Hello World” is printed to the
serial port.
The program pauses for 1000 milliseconds
loop( ) continues to loop.
RESULT
• Now let's make Arduino listen to what
we say and repeat it.
• We have to use "Serial.read" command.
THANK YOU!