What is Arduino?
Arduino is an open-source electronics platform based on easy-to-use hardware and software. It can be programmed to execute commands and make decisions based on various inputs. Arduino comes in several variants such as Nano, Uno, and Mega. They generally use Atmel controllers like ATmega8, ATmega168, ATmega328, ATmega1280, or ATmega2560.
Here, we will talk about the Arduino Nano, which uses an ATmega328p.
ARDUINO NANO HARDWARE
Talking about Arduino nano, we have various analogue, digital, and communication pins for Arduino programming. Some significant pins and their multiple functionalities are listed below.
Digital Pins
There are a total of 14 digital pins (PINs - 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, and 16) which can work with a maximum voltage of 5v(1) and a minimum voltage of 0v(0). Any voltage provided is fixed between these two levels of operation.
Analog Pins
Nano has eight analog pins numbered from A0 to A7. Each of these analog pins has a built-in ADC (Analog to Digital Converter) of bits resolution.
Serial Communication Pins-
The two serial communication pins, RX- receive and TX- transmit, are used for TTL serial data communication. The pins are connected to the pins of the USB-to-TTL Serial chip.
PWM (Pulse Width Modulation) Pins-
Pulse Width Modulation, or PWM, is used for getting analog results with digital means. Every one of these digital pins can generate a PWM signal of a specified resolution (of some bits). The PWM pin generates the PWM signal with the help ‘analogWrite()’ function. There are 5 PWM pins in nano (pins - 6, 8, 9, 12, 13, and 14).
I2C-
Pins 23, 24 as A4 and A5 are the I2C pins. It uses the I2C protocol for long-distance communication. It has two wires, one for the clock and another for data.
RESET-
Reset pins in Arduino are active LOW pins, which means if we make this pin value as LOW, i.e., 0v, it will reset the controller. Pin 28 is the reset pin in nano.
Other Pins-
3.3V: This pin outputs 3.3V.
5V: This pin outputs 5V.
GND (Ground pins): There are five ground pins on the board, i.e., considered to have a potential of 0V.
REF: It provides the voltage reference at which the microcontroller is operating.
ARDUINO SOFTWARE
Arduino supports a language known as the Arduino Programming Language or Arduino Language in general. When working with Arduino, we commonly use the Arduino IDE, which gives us two advantages:
- It is an editor with library support and a compiler.
- It uploads our program to the Arduino Board.
The Arduino Programming Language is an open framework for microcontrollers. The Arduino Programming Language Also has built-in libraries that allow to easy integration of various functionalities provided by the Arduino board.
The Arduino IDE is the text editor used for Arduino programming. It is where you can add the program (also called a sketch) to be executed through an Arduino board by uploading the program to the board.
Arduino Programming
Here's a quick look at how to get started with Arduino programming.
A sketch always contains two functions-
setup():
As the name states, it is used to set up the board. It executes all the code inside it once. setup() is used for setting up modes of pins and setting up the serial monitor.
loop():
It contains the body of the program. Everything that’s inside this function is repeated or looped continuously.
Now, let us jump on to some basic functions of Arduino, which we use often:
pinMode(pin,mode) :
This function is used to set the pin specified to behave as Input or Output.
Example – pinMode(3,INPUT) reads digital pin 3 as an input pin.
digitalRead(digital_pin) :
This function is used to read the digital signal from the specified.
Example - digitalRead(5) reads value on pin 5.
digitalWrite(pin,value) :
This function is used to write a HIGH or a LOW value to a digital pin.
Example - digitalWrite(4, HIGH) makes the pin 4 HIGH.
analogRead(analog_pin)
This function is used to read the analog signal from an analog pin and returns an integer in the range of 0 to 1023.
Example - analogRead(A3) reads the value on analog pin A3.
analogWrite(pin,value)
Value can be a number between 0 and 255.Example- analogWrite(3, 128) generates a PWM wave of 50% duty cycle on pin 3.
delay()
This function pauses the execution of the program for the mentioned amount of time. It accepts input in milliseconds, so for a value of 2000, it will wait for 2secs before the execution of the next line.
OBSTACLE SENSOR WITH ARDUINO
The following program explains the working of an IR Led and glow of led with a variable distance of the obstacle from the IR Led with the help of photodiode.
Components Used-
- Basic Red LED (5mm)
- Infrared Led (550nm)
- Photodiode
- Arduino Nano
- 220-ohm Resistor (with Red LED)
- 470-ohm Resistor(with Infrared LED)
- 10k ohm Resistor (with Photodiode)
Code:
int RedLed=5;
Int PhotoDiode=4;
int senPin=A0; // to read value of sensor
int thresValue=400; // threshold value
void setup()
{
pinMode(PhotoDiode,OUTPUT);
pinMode(RedLed,OUTPUT);
pinMode(senPin,INPUT);
digitalWrite(RedLed, LOW);
digitalWrite(PhotoDiode,HIGH); // can also use 1 instead of HIGH and 0 instead of LOW
}
void loop()
{
int value=analogRead(senPin);
if(value <= thresValue)
{
digitalWrite(RedLed,HIGH); //Red LED Glows
delay(20);
}
else if(value > thresValue)
{
digitalWrite(RedLed,LOW); //Red LED doesn’t glow
delay(20);
}
}
EXPLANATION-
This is a real-life based simulation that is a part of the Line-Following Bot. The photodiode receives reflected infrared light from the IR sensor which is read by the analog pin. The red LEDs glow or not depending on the threshold value set (thresValue), i.e., depending on whether the obstacle is close or far away.
NOTE- The amount of light reflected not only depends on the closeness of the obstacle but also the color of the obstacle i.e., a white obstacle reflects more light compared to a black obstacle.
This blog has been submitted by Cyborg, NIT Rourkela under the Robocraze Club Outreach Program.
Author - Jagdish