AR Unit2 Arduino Commands
AR Unit2 Arduino Commands
ROBOTS EX612
EC6 – AGNEL POLYTECHNIC,VERNA, GOA
2022-23
SANGEETA DOIPHODE
COURSE OUTCOMES
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
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.
// for comments
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
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
• 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
OPERATING
BOARD USABLE PINS MAX RESOLUTION
VOLTAGE
Uno 5 Volts A0 to A5 10 bits
Mini, Nano 5 Volts A0 to A7 10 bits
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
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
• 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!