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

AR Unit2 Arduino Commands

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

AR Unit2 Arduino Commands

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

AUTONOMOUS

ROBOTS EX612
EC6 – AGNEL POLYTECHNIC,VERNA, GOA
2022-23
SANGEETA DOIPHODE
COURSE OUTCOMES

• CO1 - Understand the concept of Arduino processor and IOT


• CO2 - Apply the basic protocol and working principle of interfacing modules
• CO3 - Analyse various application programs for a wide range of Arduino processors
• CO4 - Design, debug and troubleshoot Arduino/IOT based project
UNIT 2 - INTRODUCTION TO ARDUINO PROGRAMMING

08 hours – 14 marks
2.1 Block diagram, input and output pins of Arduino Uno development board
2.2 Basic commands of Arduino programming: void setup(), void loop(),
pinMode()
2.3 Basic commands for serial communication, analog input/output, digital
input/output,delay commands
FUNCTIONS - ARDUINO BOARD

Digital I/O Zero, Due & MKR Family Advanced I/O


digitalRead() noTone()
analogReadResolution()
digitalWrite() pulseIn()
analogWriteResolution()
pulseInLong()
pinMode()
Time
shiftIn()
Analog I/O
delay() shiftOut()
analogRead()
delayMicroseconds() tone()
analogReference()
micros() USB
analogWrite()
Keyboard
millis()
Mouse
FUNCTIONS - ARDUINO BOARD

Math Trigonometry External Interrupts


abs() cos() attachInterrupt()
constrain() sin() detachInterrupt()
map() tan() Interrupts
max() Communication interrupts()
min() Serial
noInterrupts()
pow() SPI
Random Numbers
sq() Stream
random()
sqrt() Wire
randomSeed()
FUNCTIONS - ARDUINO BOARD

Bits and Bytes Characters Characters


bit() isAlpha() isLowerCase()
bitClear() isAlphaNumeric() isPrintable()
bitRead()
isAscii() isPunct()
bitSet()
isControl()
bitWrite() isSpace()
isDigit()
highByte() isUpperCase()
lowByte() isGraph()
isWhitespace()
isHexadecimalDigit()
BASIC COMMANDS OF ARDUINO PROGRAMMING

Arduino expects two functions to exist - setup() and loop()


void setup()
• This is where you place all the initialization code that you want to execute once
at the beginning of your program.
• The code has instructions that set up the board before the main loop of the
sketch starts
• This function will only run once, after each power-up or reset of the Arduino
board
BASIC COMMANDS OF ARDUINO PROGRAMMING

void loop()
• This function contains the core of the program i.e. main code of the sketch,
• It is executed over and over again until the board is switched off
• On power-up of the board, the code runs
• To stop this code from running, turn the board off
VARIABLES - ARDUINO DATA TYPES AND CONSTANTS.

Constants Conversion Conversion


HIGH | LOW
(unsigned int) float()
INPUT | OUTPUT | INPU
T_PULLUP (unsigned long) int()
LED_BUILTIN byte() long()
true | false char() word()
Floating Point Constants
Integer Constants
VARIABLES - ARDUINO DATA TYPES AND CONSTANTS.

Data Types Data Types Data Types Variable Scope &


Qualifiers
double string
array
const
String()
bool float scope
unsigned char
boolean int static
unsigned int
byte long volatile
unsigned long
Utilities
char short void
PROGMEM
size_t word
sizeof()
CONST DECLARATION,VARIABLES, COMMENTS

const int LED = 13;


• LED is an integer constant number 13 which the Arduino writes every time the
word LED appears
• It specifies that the LED is connected to the Arduino pin 13

Variable declaration: keywords int, float, char

// for comments
DATA TYPES AVAILABLE IN ARDUINO

• boolean (true or false)


• char (single character with values:128 to 127)
• byte (number between 0-255; use only one byte of memory)
• int (uses 2 bytes of memory to represent a number between –32,768 to 32,767;
it’s the most common data type used in Arduino)
DATA TYPES AVAILABLE IN ARDUINO

• unsigned int (uses 2 bytes but the unsigned prefix means that it can store only
positive numbers, in the range 0 to 65,535)
• long (twice the size of an int and holds numbers from –2,147,483,648 to
2,147,483,647)
• unsigned long (unsigned/positive version of long; it goes from 0 to
4,294,967,295)
DATA TYPES AVAILABLE IN ARDUINO

• float (floating-point values, uses 4 bytes of RAM)


• double (double-precision floating-point numbers with a maximum value of
1.7976931348623157 x 10308)
• string (set of ASCII characters used to store textual information; use one byte
for each character in the string, plus a null character to tell Arduino that it’s the
end of the string)
• array (A list of variables that can be accessed via an index; uses [ ] and { }
STRUCTURE / ELEMENTS OF CODE

Sketch Further Syntax Control Structure


break
loop() #define (define)
continue
setup() #include (include)
do...while
/* */ (block comment) else
// (single line comment) for
; (semicolon) goto
if
{ } (curly braces)
return
switch...case
while
STRUCTURE – OPERATORS IN CODE

Arithmetic Operators Comparison Operators


% (remainder) != (not equal to)
* (multiplication) < (less than)
+ (addition) <= (less than or equal to)
- (subtraction)
== (equal to)
/ (division)
> (greater than)
= (assignment
>= (greater than or equal to)
STRUCTURE – OPERATORS IN CODE

Boolean Operators Bitwise Operators


! (logical not) & (bitwise and)
&& (logical and) << (bitshift left)
|| (logical or >> (bitshift right)
Pointer Access Operators ^ (bitwise xor)
& (reference operator) | (bitwise or)
* (dereference operator)) ~ (bitwise not)
STRUCTURE – OPERATORS IN CODE

Compound Operators Compound Operators


%= (compound remainder) -- (decrement)
&= (compound bitwise and) -= (compound subtraction)
*= (compound multiplication) /= (compound division)
++ (increment) ^= (compound bitwise xor)
+= (compound addition) |= (compound bitwise or)
CONTROL STRUCTURES

If( ) { - } . . . else { - - } switch( )


{
• Structure for decision-making
case –
• If the expression within () is true, the case –
following code { } will be executed }
• If expression within () is false, the block of
code following else { - - } will be executed. • break
• if may be used without else clause
• continue
• else cannot be used without if
CONTROL STRUCTURES

Looping structures, to repeat a block of code a specified number of times/ until


condition is true:
• for(loop var initialization; condition; loop var change) { - - }
• while(condition) { - - }
• do { - - }while(condition);

• return; //to return from a called function


DIGITAL INPUT / OUTPUT COMMAND/ FUNCTION

digitalWrite(pin, high/low value)


• This function is able to send high/low to any pin that has been configured as output
• The first argument specifies the Arduino pin
• The second argument indicates the pin level high (+5V or +3.3V) or low (0V)
• Eg. digitalWrite(LED,HIGH); // This turns the LED pin high/on;
• Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up
resistor, which acts like a large current-limiting resistor
DIGITAL INPUT / OUTPUT COMMAND/ FUNCTION

digitalRead(pin)
• This function reads the state/value of the digital input pin specified within parentheses;
• It returns a high/low value depending on voltage level read at the specified pin
• Eg.
const int BUTTON = 7;
val = digitalRead(BUTTON); // read input value from pin 7 & store in variable val
PIN MODE SETTING COMMAND/ FUNCTION

pinMode(pin, mode input/output)


• To configure/set the specified digital pin as input or output
• Eg. pinMode(LED, OUTPUT); //sets the digital pin named LED as output
• As of Arduino 1.0.1, it is possible to enable the internal pullup resistors with the
mode INPUT_PULLUP
• Additionally, the INPUT mode explicitly disables the internal pullups
ON/OFF SENSORS

• Pushbutton
• Switches
• Thermostats (a switch that opens when the temperature reaches a set value)
• Magnetic switches (also known as “reed relays” - have two contacts that come
together when they are near a magnet; used by burglar alarms to detect when a window
is opened)
• Carpet switches (small mats that you can place under a carpet or a doormat to detect
the presence of a human being
ON/OFF SENSORS

• Tilt switches (a simple electronic component that contains two contacts and a
little metal ball; inside of a typical model.
Eg. tilt sensor - when the sensor is in its upright position, the ball bridges the two contacts,
and this works just as if you had pressed a pushbutton. When you tilt this sensor, the ball
moves, and the contact is opened,which is just as if you had released a pushbutton.
Using this simple component, you can implement, for example, gestural interfaces that react
when an object is moved or shaken
ANALOG INPUT / OUTPUT

• Analog sensors output a continuously changing value, which is read on analog input pins. Eg.
Thermistor, photoresistor
• Analog I/O functions:
▪ analogRead(pin)
▪ analogWrite(pin, value 0-255)
ANALOG INPUT / OUTPUT

analogRead(pin)
• To read the voltage at the specified analog input pin
• Pin is the name of the analog input pin to read from (A0 to A5 on most boards, A0 to A6
on MKR boards,A0 to A7 on the Mini and Nano,A0 to A15 on the Mega)
• This function returns an integer number between 0 and 1023, which represents
voltages between 0 and +5V or +3.3V,with 10-bit ADCs
• On 12-bit ADC boards,it returns 0-4095 for voltages between 0 and +5V or +3.3V
• Eg. analogRead(0) –> reads voltage at analog input pin 0
ANALOG INPUT / OUTPUT

• Arduino boards contain a multichannel, 10-bit analog-to-digital converter. Thus, on


an Arduino UNO, resolution is 5V/1024 units or 0.0049 volts i.e. 4.9 mV per unit
• The input range can be changed using function analogReference()
• The resolution can be changed (to 12 bits, only for Zero, Due and MKR boards) using
function analogReadResolution()
• On Atmega-based boards (UNO, Nano, Mini, Mega), it takes about 100 microseconds
(0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a
second
ADC ON DIFFERENT ARDUINO BOARD

OPERATING
BOARD USABLE PINS MAX RESOLUTION
VOLTAGE
Uno 5 Volts A0 to A5 10 bits
Mini, Nano 5 Volts A0 to A7 10 bits

Mega, Mega2560, MegaADK 5 Volts A0 to A14 10 bits

Micro 5 Volts A0 to A11* 10 bits


Leonardo 5 Volts A0 to A11* 10 bits
Zero 3.3 Volts A0 to A5 12 bits**
Due 3.3 Volts A0 to A11 12 bits**
MKR Family boards 3.3 Volts A0 to A6 12 bits**
ANALOG INPUT / OUTPUT

analogWrite(pin, value 0-255)


• To write/send a voltage on the specified analog output pin
• This function accepts a number between 0 and 255, which represents voltages between
0 and 5 volts
• Eg. analogRead(0,255) –> writes voltage +5V at analog output pin 0
DELAY COMMANDS

delay(ms);
• The processor does nothing for the amount of milliseconds that you pass as an argument in
() i.e. wait for ___ milli seconds
• delay(1000); wait for 1000 milli seconds = 1s
ARDUINO - SERIAL COMMUNICATION

• Arduino has a USB connection used by the IDE to upload code into the processor
from the computer
• This connection can also be used by the Arduino sketches to send data back to the
computer or to receive commands from it
• We use a serial object (a collection of capabilities that are bundled together for the
convenience of people writing sketches) which contains all the code that we need to send and
receive data
• After uploading the code to the Arduino, press the “Serial Monitor” button on the
Arduino IDE;You see the output.
COMMANDS FOR SERIAL COMMUNICATION

Serial.begin(bps);
• To open the serial port to send data back to the computer at specified bits per second;
• It is written in the function void setup(){ }
• Eg. Serial.begin(9600); // baud rate 9600 bps
COMMANDS FOR SERIAL COMMUNICATION

Serial.print(“ ”);
• To send/print the message within “ ” to the serial port
• It is written in the function void loop(){ }

Serial.println(val);
• To send/print the value of variable val to the serial port
• It is written in the function void loop(){ }
PWM - PULSE WIDTH MODULATION
PROGRAM CODE – BLINKING LED AT PIN 13

const int LED = 13; // LED connected at pin 13


void setup() {
pinMode(LED, OUTPUT); // Pin 13 LED is output
}

void loop() {
digitalWrite(LED, HIGH); // turn LED ON
delay(1000); // Wait for 1 second
digitalWrite(LED, LOW); // turn LED OFF
delay(1000); // Wait for 1 second
}
TURN ON LED WHEN BUTTON PRESSED

const int LED = 13; // LED connected to pin 13 void loop() {


const int BUTTON = 7; // Input button at pin 7 val = digitalRead(BUTTON); // read input value & store
int val = 0; // val stores state of input pin // check whether the input is 1/button pressed
if (val == HIGH) {
void setup() { digitalWrite(LED, HIGH); // turn LED ON
pinMode(LED, OUTPUT); // Pin 13 LED is output
}
pinMode(BUTTON, INPUT); // Pin 7 BUTTON is input else {
} digitalWrite(LED, LOW); }
} //end of function loop()
TURN ON LED WHEN BUTTON PRESSED AND RELEASED

const int LED = 13; // LED connected at pin 13 void loop() {


const int BUTTON = 7; // Input button connected to pin 7 val = digitalRead(BUTTON);
int val = 0; // val used to store state of the input pin if ((val == HIGH) && (old_val == LOW))
int state = 0;
state = 1 - state;
int old_state = 0;
old_val = val;
if (state == 1)
void setup() {
pinMode(LED, OUTPUT); // Pin 13 LED is output digitalWrite(LED, HIGH); // turn LED ON
pinMode(BUTTON, INPUT); // Pin 7 BUTTON is input else
} digitalWrite(LED, LOW); // turn LED OFF }
}
TURN ON LED WHEN BUTTON PRESSED/RELEASEDWITH
DEBOUNCING
const int LED = 13; // LED connected at pin 13 void loop() {
const int BUTTON = 7; // Input button connected to pin 7 val = digitalRead(BUTTON);
int val = 0; // val used to store the state of the input pin if ((val == HIGH) && (old_val == LOW)) {
int state = 0; state = 1 - state;
int old_state = 0; delay(10); }
old_val = val;
void setup() {
if (state == 1)
pinMode(LED, OUTPUT); // Pin 13 LED is output
digitalWrite(LED, HIGH); // LED ON
pinMode(BUTTON, INPUT); // Pin 7 BUTTON is input
else
}
digitalWrite(LED, LOW); // LED OFF
}
REFERENCES

• https://www.arduino.cc/reference/en/
• https://docs.arduino.cc/learn/microcontrollers/digital-pins
• https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/
THANK YOU!

You might also like