Basics To Arduino and Raspberry Pi

Most of the computing devices we use (including phones, tablets and games consoles) are designed to stop us from tinkering (experiment) with them. You can only use them in the predefined ways that the company offers to i.e. make calls, take photos, etc. You can’t enhance its functionality in terms of adding an extra hardware such as a fingerprint sensor. To do that you need some interfaces (inside the phone) to which you could connect the fingerprint sensor which is not readily available inside the main circuit board of the phone. Then what to do?

Fortunately, those days are gone when there was a considerable gap between a mobile company manufacturer and an electronic hobbyist. You don’t need to have a PhD or any formal type of higher studies to master the concepts of building your own mobile or a portable laptop. Thanks to Raspberry Pi and Arduino.

Raspberry Pi and Arduino are two open source hardware platforms which enables us to build/hack any electronic/electrical device and use them according to our needs. The idea is that the user provides the input and his imagination and these platforms gives us our desired result. These hardware platforms provides us a simple and low cost recipe that we can use to experiment with and play with our own ideas.

Over the years they have been the brain of thousands of projects, from everyday objects to complex scientific instruments. A worldwide community of makers – students, hobbyists, artists, programmers, and professionals – has gathered around theses open-source platforms, their contributions have added up to an incredible amount of accessible knowledge that can be of great help to novices and experts alike.

Now one may ask, why Arduino/Raspberry Pi only? It’s because they have got some serious advantages one of them being, they can interact with LEDs, motors,Speakers,GPS, cameras,smartphone,TV and any electronic/electrical device you can think of with great ease.

Let’s start with Arduino first. A big advantage that the Arduino has is the environment in which the commands are given to do specific tasks. Commands are nothing but some lines of English like language which have a definite syntax (pattern) as described by the software in which you will write the lines of commands. Now I am sure you are confused with the term ‘Environment’. Let’s try to understand with the help of an example.

Consider two environment: – A school environment and your own home environment. Certainly, inside your house in your home environment you would feel quite free to do any task as you want to. You won’t need to focus on external disturbances unlike the school environment in which it becomes quite time consuming. Here in your home environment each and every task becomes easy to achieve.

Likewise here in Arduino, it uses a home environment called the Arduino programming Environment which is nothing but a software which can be installed in a laptop. Here because of this user friendly software we can write complex tasks (written in English like language) very much easily.

Before we take an in-depth analysis let us 1st understand what each and every component of an Arduino does-

  1. Micro-controller- It’s the brain of the circuit board. It processes the given tasks and executes it to give an output.
  2. Input/output Pins- They are nothing but some pathways with help of which information is sent to the micro-controller or information is sent out from the micro-controller.
  3. DC input- The power supply needed for the Arduino to work.
  4. USB port- The port through which we can connect the Arduino to the laptop.
  5. Reset Switch- To reset the program which we had executed inside the micro-controller.

Consider your own family consisting of your father, mother, yourself and the servants. Here your father is the head person who attends each and every tasks of the family and processes them to give an advice to the desired person. But he doesn’t do all the tasks by himself, he just orders the servants to do the tasks. So you can consider the father as the micro-controller. Now, the servants when they start doing the tasks given to them and individually produces some output (work) to the external members of the family like your neighbour then these servants behaves like the output pins to the micro-controller (father). Again, after the father has given a task to the servant to analyze something from the external member of the family, if he reports back a result to the father than this servants becomes the Input pins to the micro-controller (father). The language that you speak with your family can be compared with the Arduino programming language which is used by the micro-controller to communicate with the input and output pins. An Arduino R3 model is shown below-

Let us build our 1st project of blinking an L.E.D. Here the micro-controller turns on the LED ON for one second, then OFF for one second, repeatedly. First take an L.E.D and connect the shorter leg (-ve) to the GND (ground) which is located to the left side of PIN 13. And connect the +ve terminal of the LED to the pin 13(See the upper figure for the pin’s position). Then connect the USB to the computer with the help of a USB cable. As soon as the Arduino is powered up, open the Arduino IDE software and write the following program-

void setup()
{
    pinMode(13, OUTPUT);
}

void loop()
{
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
}

Explanation: – Every Arduino program has two main functions called setup() and loop(). The setup() runs only once when the Arduino is powered on. The loop() runs continuously after the setup() has completed (Please note that the micro-controller reads the lines step by step. That’s why the micro-controller will read first the voidsetup part followed by the voidloop part). When we write pinMode it means we will be assigning a specific pin either as OUTPUT or INPUT. The reason as to why we have written the pinMode under the setup is because we need to tell the microcontroller that the pin 13 is to be used as an output only once. Once the microcontroller understands that it needs to use the pin 13 as an output it will keep on thinking that it is an output throughout the execution of the current program. Also you may ask as to why we have used the pin 13 as an output. The answer is simple. The microcontroller will send a +5 Volt as an output to the LED but if we had used a fingerprint sensor then it would have been an INPUT to the microcontroller i.e. pinMode(13, INPUT). Again, since our project demands us to blink the LED so we need to send +5v PULSES after a short duration gap in between in order to turn the LED OFF or ON. Here, when we write digitalWrite it means for the current instant the microcontroller needs to turn ON pin 13 i.e. give a +5v as an output through pin13 which in turn is connected to the +ve terminal of the LED and we can see the LED glowing and thus we write digitalWrite(13, HIGH). As we know to blink the LED you need to turn off the LED too. So we write digitalWrite(13, LOW) which means for the current instant we are turning OFF the LED. Also note there are two delays here. The first delay is to make the LED glow for 1000ms (1s) and the next is to make the LED dim for 1000ms (1s).

Thus when we click the UPLOAD option this program gets uploaded to microcontroller with the help of the USB cable. Congrats, you have made your 1st Arduino project.

Let us proceed to the next part of our discussion- The Raspberry PI(RPi)

Raspberry Pi (RPi) is a tiny and affordable computer consisting of a single, credit card-sized circuit board. You can use it to do almost anything that is doable with a regular desktop computer, such as running desktop applications and playing music files. Raspberry Pi also has great potential for developing automation systems using sensors, relays, lights, and motors due to its small size, low power requirements, and small price tag.

Before beginning with RPi you should first know the difference between Arduino and RPi.

An Arduino board is best used for simple repetitive tasks: opening and closing a garage door, reading the outside temperature, driving a simple robot, etc.

Raspberry Pi is best used when you need a full-fledged computer: driving a more complicated robot, performing multiple tasks, doing intense calculations.

Actually, there is a simple rule which will help us to decide in which project we are to use which hardware platform. The rule is stated below-

“If you can describe your project with less than two ‘ANDs’ then go for Arduino. If you need to use more than two ‘ANDs’ then it would be better to go for Raspberry Pi. A simple example would make this rule easier. If you want to monitor the water level of a tank and when full then send a message to your mobile, Arduino is the 1st choice. It’s because this project uses a single ‘AND’.

Again, if we need to monitor the water level and also monitor the total litres that has been pumped out of the pump and then comparing the total water consumption for today with the rest of the days and if the water consumption for today is greater than yesterday then switch the pump off regardless the tank is full or not and then store the data in an online server for future reference then Raspberry Pi would undoubtedly be the best!!!! As we see here, we are using more than two ‘ANDs’ and so RPi is chosen.

Now, let us see the different parts of the Raspberry Pi in reference with a general laptop-

Raspberry Pi 3 General Laptop
Central Processing Unit(CPU) 1.2GHz 64-bit quad-core ARMv8 Intel Core 2 duo,I3,I5,I7,AMD
RAM 1GB More than 1GB
Graphics Processor(GPU) Broadcom VideoCore IV Nvidia Geforce,AMD radeon
WIFI 802.11n Wireless LAN 802.11n Wireless LAN
HDMI Yes Yes
Bluetooth Yes Yes
General Purpose Input/output Pins(GPIO) 40 Not available on board
LAN Yes Yes
Camera Interface(CSI) Yes Yes
Display Interface(DSI) Yes Yes
3.5mm Audio Jack Yes Yes
Internal Storage microSD Card Hard Disk
Weight ~50gm ~1000gm
Size Credit Card Much larger
Input Voltage 5volt D.C 19V D.C

*Please note that the size and weight of the general laptop is calculated including the on board RAM, hard disk, camera and all the minimum requirements to run the computer with an Operating System installed. The raspberry pi has all the features inbuilt on the motherboard and that’s why it is so small yet powerful.

That’s all you need to know about raspberry pi for now. We hope we have successfully made you a little interest in this two platforms. There are tons of tutorials present in the net. We could have written more about raspberry pi but actually, a little knowledge of LINUX operating system and networking is needed to work in this platform. You are most welcome to send us your queries, project ideas at jistehc2008@gmail.com or follow us at www.facebook.com/ehcjist

Advertisements

Controlling a Servo using a 10k pot and Arduino

Introduction

Here we will control a servo by rotating a 10k potentiometer, with Arduino Uno as controller. But before going through this tutorial, i suggest you go through this basic introductory article on Servos.

The 10k pot is connected to one of the analog pins and the servo is connected to one of the pwm pins of the arduino board.

A servo can be made to turn to any angle between 0 to 180 degrees by using Arduino’s inbuilt Servo library.

Here we map the analog reading between 0-1023 obtained from the 10k pot to an angle between 0 – 90 degrees, by using arduino’s inbuilt map() function and use the mapped value to run the servo. This makes the servo rotate  in accordance with the rotation of the 10k pot.

Breadboard Setup

Arduino-servo-pot-interfacing_bb.png
Breadboard setup

 

Note: A servo has three wires +VCC, GND and SIGNAL . Different servos use different color codes to indicate +VCC, GND and SIGNAL .

a4228312-146-user38_pic55606_1294883044
Servo Color codes

Schematic Diagram

Arduino-servo-pot-interfacing_schem
Schematic diagram

Code

#include<Servo.h>

Servo myServo; //Declaring a Servo object
int servoPin = 10; //declaring a variable to store the pin number to which the servo is connected
int potValue = 0; //declaring a variable to store potentiometer analog value
int potPin = 0; //10k pot connected to pin 0 of analog port
int servoPosition = 0; //declaring a variable to store the angle for servo rotation
void setup() {
  // put your setup code here, to run once:
  myServo.attach(servoPin); //attaching the servo object to the servo pin
}

void loop() {
  // put your main code here, to run repeatedly:
 potValue = analogRead(potPin); //get the analog value of 10k pot
 servoPosition = map(potValue,0,1023,0,180); //map the analog value of the 10k pot from a value between 0 - 1023 to a value between 0 - 180 and store it in a variable
 myServo.write(servoPosition); //turn the servo by the angle value stored in servoPosition variable
 delay(100);

}

 

 

 

Interfacing HC-SR04 Ultrasonic Sensor with Arduino

hc-sr04 sensor

Introduction

HC-SR04 is an ultrasonic sensor which works on the principle of SONAR to calculate distances from a far object. The HC-SR04 can be used to detect objects in the range of 2cm to 4m. Since it works on the principle of reflection of sound waves, its operation is unaffected by sunlight as opposed to IR Sensors. As such it can be used to detect and measure distances from objects very reliably. But acoustically soft materials like cloth can be difficult to detect.

Technical Specifications

  • Power Supply : +5V
  • Quiescent Current : <2mA
  • Working Current: 15mA
  • Effectual Angle: <15°
  • Ranging Distance : 2cm – 400 cm/1″ ­ 13ft
  • Resolution : 0.3 cm
  • Measuring Angle: 30 degree
  • Trigger Input Pulse width: 10uS
  • Dimension: 45mm x 20mm x 15mm

ultra-sonic-dimen

Pin Details

  • VCC : +5V
  • Trig : Trigger input Sensor
  • Echo : Echo output Sensor
  • GND : GND

How it works

To start measurement, Trig pin must receive a pulse of high (5V) for at least 10us, this will initiate the sensor, and the sensor will transmit out 8 cycles of ultrasonic pulses at 40kHz and wait for the reflected ultrasonic pulse. When the sensor detects reflected ultrasonic pulses in the receiver, it will set the Echo pin to high (5V) and delay for a period (width) which is proportional to the distance from the object.

To obtain the distance,  we measure the width of the pulse at the Echo pin and multiply it with the speed of sound (340 m/s) and divide it by two.

hc-sr04-timing-diagram

Interfacing HC-Sr04 with with Arduino

 

arduino_hc-sr04_interfacing_bb.png
Breadboard Layout
arduino_hc-sr04_interfacing_schem
Schematic diagram

Arduino Code

/* HC-SR04 Sensor

This sketch reads a PING))) ultrasonic range finder and lights up multiple
led s in direct proportion to the distance of the given object from the
sensor. To do this, it sends a pulse to the sensor to initiate a reading,
then listens for a pulse to return. The length of the returning pulse is
proportional to the distance of the object from the sensor.

The circuit:
* VCC connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* TRIG connection of the PING))) attached to digital pin 3
* ECHO connection of the PING))) attached to digital pin 2

Created 31 Jan 2016
by Sandhan Sarma
This example code is in the public domain.

*/

// this constant won't change.It's the pin number
// of the sensor's output:
const int trigPin = 3;
const int echoPin = 2;
const int ledPin1 = 8;
const int ledPin2 = 7;
const int ledPin3 = 6;
const int ledPin4 = 5;
const int ledPin5 = 4;

void setup()
{

//set output pins for leds
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(ledPin4,OUTPUT);
pinMode(ledPin5,OUTPUT);

//set pin modes for Trigger and Echo pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

//initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, distance_in_cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);

// The echoPin pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
distance_in_cm = microsecondsToCentimeters(duration);

//This block of code will print out the distance
//to the Arduino IDE serial monitor
Serial.print(distance_in_cm);
Serial.print(&amp;quot;cm&amp;quot;);
Serial.println();

//The following if-else block will sample the distance
//at every 10 cm and light up an Led.
//The farther the object, more number of Led s glow
//nearer the object lesser led s glow
if (distance_in_cm&amp;amp;gt;=10)
{
digitalWrite(ledPin5,HIGH);
}
else
{
digitalWrite(ledPin5,LOW);
}

if (distance_in_cm&amp;amp;gt;=20)
{
digitalWrite(ledPin4,HIGH);
}
else
{
digitalWrite(ledPin4,LOW);
}

if (distance_in_cm&amp;amp;gt;=30)
{
digitalWrite(ledPin3,HIGH);
}
else
{
digitalWrite(ledPin3,LOW);
}

if (distance_in_cm&amp;amp;gt;=40)
{
digitalWrite(ledPin2,HIGH);
}
else
{
digitalWrite(ledPin2,LOW);
}

if (distance_in_cm&amp;amp;gt;=50)
{
digitalWrite(ledPin1,HIGH);
}
else
{
digitalWrite(ledPin1,LOW);
}
delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

References