A Quick Start Guide to Arduino and its Working

You must have heard people working and creating new innovative projects using something called an Arduino or Rasberry Pi. Dumbstruck and want to start working on such cool stuff, but don’t know where to start? This guide will help you in getting started with the basics of Arduino and its working.

Arduino Uno

Arduino

First things first, what is Arduino? Arduino is an open-source hardware and software company, project and user community that designs and manufactures single-board microcontrollers and microcontroller kits for building digital devices.

Atmega328p
ATmega328p

One of the most commonly used board among them is the Arduino Uno which we will be using in the demonstration. Anyways, what is a microcontroller? It is a single chip which contains different components such as a microprocessor (CPU), ROM, RAM for processing the data, a clock and an I/O control unit. It can be programmed to do various functions via a programmer(IDE). One such chip is Atmega328p which is installed inside Arduino Uno.

Arduino Uno

Arduino Uno is a development board which consists of not only a microcontroller, but also some other peripherals such as a USB port, input/output pins, power supply etc. Different development boards consist of a different combination of peripherals and thus users can choose one depending on their needs.

Arduino Uno Pinout

Specifications of the Uno board are as follows:

  • It is powered by 5V source which can be supplied by a PC via USB port or Vin(external power source) pin or the DC power jack (7–12V)
  • It can also provide power supply to sensors connected to it via its 5V and 3.3V pins and GND pins.
  • There are 6 analog pins A0-A5 which can be used for analog input from a sensor since it has a built-in ADC which converts analog value to digital value.
  • There are 14 digital I/O pins out of which 6 are PWM pins marked with ‘~’ which are used to convert the digital signal into analog by varying the width of the pulse.
  • The USB port is also used to upload the program(often called a sketch) on the Arduino board which is written in C++.
  • A reset button is also provided to reset the Arduino.
  • It uses UART serial communication protocol.

Programming Arduino Uno

Before moving further, install Arduino IDE from here and have the following components ready:

  1. Arduino Uno
  2. USB type A to type B cable
  3. Ping sensor(ultrasonic sensor)
  4. Jumper wires
  5. PC

Initial setup:

  1. Connect the cable to your Arduino and your laptop.
  2. Open Arduino IDE on your laptop.

On opening the Arduino IDE, this is the first screen you will see. Void setup() is the function which is called when the Arduino is powered for the first time and is used to define pin modes, variables, baud rate etc.

Void loop() is the function which is executed indefinite no of times. For eg: reading the value measured by the sensor.

It can be seen that the name of the program file is sketch_date, which is the default name of the file when a new Arduino file is created.

  • Connect the Arduino to the PC via USB cable.
  • Connect the Vcc pin of ping sensor to 5V pin and Gnd pin to GND pin of Arduino. Almost every sensor will have a Vcc and Gnd pin which you can connect to the Arduino as mentioned above.

Ping sensor

It is used to calculate distance from an object. Its principle is that it transmits an ultrasonic pulse(40,000 Hz) which is reflected by the obstacle in its path back to the sensor. The distance of the object is calculated by the time it takes to return to the sensor.

Distance = (speed of sound * time delay) / 2

where the speed of sound is taken in cm/s since the time delay is taken in form of microseconds.

Since the distance covered by the wave is twice(back and forth), the distance is divided by 2.

Circuit Diagram

Program Souce Code and its explanation

// defines pins numbers
int trig = 3;
int echo = 2;

// defines variables
long duration;
int distance;
void setup()
{
pinMode(trig, OUTPUT); // Sets the trigPin as an Output
pinMode(echo, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
// Clears the trigPin
digitalWrite(trig, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echo, HIGH);
distance= duration*0.034/2;
Serial.print(“Distance: “);
Serial.println(distance);
}

As we saw earlier, there were 2 pins, trig and echo pins, which were not connected to the Uno board, are now made connected according to the code we write. Trig pin is defined as a variable with a value equal to 3. It means that data which is read through digital pin 3 will be denoted by the name ‘trig’ and similar is for echo pin which is read via pin 2.

If you have a sensor that reads analog data then the analog pin for that sensor can be defined by using the following syntax:

#define variable_name A0 //A0 is analog pin no

Variables such as distance and duration are defined in the default way as you define them in C++.

In void setup() we need to define whether the pin we have defined is input pin or output pin.In our case trig pin is used to sent out the ultrasonic pulse so it is defined as Output and is received via echo pin so it is defined as input pin.

Next thing we need to define is the baud rate. It is basically the rate at which serial communication happens between the Arduino and the serial monitor and is usually defined as 9600 bps(bits per second).

Settings defined in the setup function are executed only once when the Arduino is powered up.

In void loop() the working of the ping sensor will be defined.digitalWrite() and digitalRead() are the functions used to write and read the data of digital pins. For analog sensors data can be read and written using analogRead() and analogWrite() functions.Only HIGH and LOW can be written to digital pins .

To clear the trig pin, it is set to LOW for 2s and then it is set to HIGH for 10s which will send 8 sonic bursts at 40 kHz and then again to LOW.

Fact: The 8 pulse pattern differentiates the ping sensor waves from other ambient ultrasonic noise and is thus is easily recognized by the receiver of the sensor.

Duration in which the pulse is reflected back is measured via the pulseIn() function. Since the input is HIGH, the pulseIn() will start the timer when it goes HIGH due to reflected pulse and will stop when it goes LOW when the reflected pulse stops

It is due to the limitation that there can be errors if the pulses are shorter than 10s, and thus this value was chosen for the delay. This duration value (in s) is fed to the distance formula and thus the distance is calculated.

In order to print the message serial.print() function is used.If you want to give newline after each output then you can go for serial.println().

Program execution

Upload(arrow icon) the sketch on the Uno board and the output can be seen on the serial monitor(magnifying icon).

Note: Examples of connecting different peripherals are also given in the IDE and can be seen by going to Files →Examples

Common errors you should avoid

  • Misconfiguration of the pins which gives you erroneous readings.
  • Syntax errors in the code such as missing ‘;’ incomplete parenthesis etc.
  • There may be projects where you need to install libraries so make sure to include them.
  • While uploading there maybe error where COM port is not recognized so select the right port by going to Tools >Port

Happy Learning!

Author: Anjan Agrawal

--

--