Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Arduino uses a simplified version of C++ as its programming language. It provides an easy-to-understand environment for writing code, especially for beginners.
The Arduino IDE (Integrated Development Environment) is the software application used to write, compile, and upload code to Arduino boards. It’s designed to be simple and user-friendly, making it accessible for beginners, but also powerful enough for more advanced users.
Download the IDE from the official Arduino website and install it on your computer.
Arduino programs, often called sketches, follow a basic structure with two main functions – setup() and loop(). You will see these two blocks when you open Arduino IDE.
Click File -> Save -> Type the name -> Save.
We use Arduino Uno in this project. Click Tools -> Board -> Arduino AVR Boards -> Arduino Uno.
Click Tools -> Port -> COM3.
The port number is not fixed. The “COM” port number shown in the Arduino IDE is assigned by the operating system when the Arduino is plugged in and can differ across different computers or even different USB ports on the same computer.
Click the Verify icon in the upper left corner.
After checking the errors, click the Upload icon in the upper left corner.
You can open and close the Output panel by clicking the Toggle Bottom Panel icon in the lower right corner.
Serial Monitor is a tool for viewing and interacting with data sent between the computer and the Arduino board. It is useful for debugging or monitoring sensor readings in real time.
Click the Serial Monitor icon in the upper right corner.
There are several ways to declare pins.
int ir_sensor = 2
Create a variable ir_sensor of the int data type and assign it 2. The variable can be modified throughout the program.
const int ir_sensor = 2
Create a variable ir_sensor of the int data type and assign it 2. However, you can not change the assigned value.
#define ir_sensor 2
#define is a preprocessor directive that performs text replacement. It does not consume memory since the value is replaced during compilation.
#define creates a constant value that cannot be changed during runtime.
This function runs only once when the program starts. It’s used to initialize variables, pins, and peripherals.
void setup()
{
Serial.begin(9600);
pinMode(ir_sensor, INPUT);
pinMode(buzzer, OUTPUT);
}
This function runs continuously in a loop, making Arduino constantly monitor and control its inputs and outputs.
This function assigns a specific pin as either INPUT or OUTPUT.
Syntax
pinMode(pin, mode);
pinMode(2, INPUT); //sets pin 2 as an input pin
pinMode(11, OUTPUT); //sets pin 11 as an output pin
This function is used to read the value of a digital pin, either HIGH or LOW. It is commonly used to check the state of a button, switch, or other digital sensor connected to an Arduino pin.
Syntax
digitalRead(pin);
The digitalRead() function returns one of two values:
digitalRead(5); //reads the value on pin 5
This function is used to set the voltage of a digital pin to either HIGH (5V) or LOW (0V). It controls the state of a pin, either turning it on or off, typically used for LEDs, motors, or other digital components.
Syntax
digitalWrite(pin, value);
digitalWrite(13, HIGH); //makes the pin 13 HIGH
digitalWrite(13, LOW); //makes the pin 13 LOW
This function is used to read the value from an analog pin. It converts the analog voltage on the pin into a digital value between 0 and 1023. This is commonly used for reading values from sensors like temperature sensors, potentiometers, light sensors, etc.
Syntax
analogRead(analog_pin);
The function returns an integer value in the range of 0 to 1023.
analogRead(A2); //reads the value on analog pin A2
This function is used to output a simulated analog signal (PWM signal) to a digital pin. Although Arduino does not have true analog output, analogWrite() creates a PWM signal that mimics an analog voltage by rapidly switching the output on and off.
Syntax
analogWrite(pin, value);
analogWrite(3, 127); //generates a PWM wave of 50% duty cycle on pin 3
This function is used to pause the program for a specific amount of time. The time is given in milliseconds (ms).
Syntax
delay(time_in_milliseconds);
delay(1000); //pauses the program for 1 second
A line comment is used to add explanatory notes within the code. Comments can be singled-lined or multi-lined.
Single-line comments start with two forward slashes (//).
// This is a comment
Multi-line comments start with /* and end with */.
/ * This is
a multiline comment*/
An Arduino library is a collection of pre-written code that simplifies the use of hardware or software components in an Arduino project. The IDE has built-in libraries for easy interfacing with various sensors, motors, displays, and other components.
Click Tools -> Manage Libraries to open the Library Manager.
We can also use the Library icon in the left column to open the Library Manager.
In the text box, type the name of the Library you want to install. Then click the INSTALL button.
In the main folder of the GitHub library, click the Code button. Choose Download ZIP and save the zip file on your computer.
In the Arduino IDE, click Sketch -> Include Library -> Add .ZIP Library. Then choose the ZIP file you saved before.
Once installed, you can include a library at the beginning of your sketch. #include is used to include outside libraries in your sketch.
#include <Wire.h>
#include <Adafruit_GFX.h>
After including the library, you can call its functions in your code.