Stage 4: Complete Beginner’s Guide For Arduino Hardware Platform For DIY

Contents

1. Background
2. Setting up Arduino Development Environment
3. Arduino Power Management
3.1 Powering Up Arduino Board
3.2 Output Power of Arduino
4. Indicators And Switches
4.1 LED
4.1.1 Working With Onboard LED
4.1.2 Connecting and Controlling External LED
4.2 Buzzer
4.3 Switches
5. Arduino With Display
6.Arduino With Sensors
   6.1 Sensor Principles
6.2 A/D Converter
6.3 Temperature Sensor
6.4 Metal Sensor
6.5 Ultrasonic Sensor With Arduino                                                          7. PWM
7.1 PWM Basics
7.2 Arduino’s PWM Support
7.3 Controlling LED Intensity through PWM                               8. Working With Relays
8.1 Relay Basics
8.2 Arduino with Relays
8.2.1 Relay Control Using Transistor
8.2.2 Relay Control using Optocoupler
9. Working With Motors
9.1 DC Motor Control using Transistors
9.2 Controlling Motors Through Relay
10. Wireless With IR Sensor                                                                11. Conclusion

1. Background

Arduino is a great embedded platform for prototyping your hobby projects. It is really easy to learn, simple yet efficient and powerful. Arduino Boards are powered by Atmel family Microcontrollers. The board as several features which are so crucial to many hardware applications which includes Digital pins,Ability to drive low power motors, PWM, ability to connect to other devices using Serial Communication, onbard A/D converter which can directly read sensor values and many more. Even though the platform is claimed to be only for prototyping, I have personally used the board for many production projects and it works quite well in those works too. In an era of Internet Of Things where main focus of development in embedded system is being shifted entirely towards developing services for the devices. However understanding and learning the hardware capabilities of these devices, ways of controlling displays, indicators, buzzers, actuators, working with sensors also plays an important role. It helps once build a conceptual framework of the capabilities of such devices which makes it easy to integrate such boards over IoT.

Even though Arduino has been around for some good time and has had gone through several changes, the core features remains same. There are many blogs and articles around that explains the working of Arduino with different hardwares, I found not a single web entry that provides an insight of the overall hardware features of the platform. Therefore for a non embedded developer( non Electronics developer), learning the hardware part becomes a real stiff learning curve. In this Tutorial I would try to provide an insight into overall hardware capabilities of Arduino with most common hardware required to develop real time projects with the platform. This tutorial is aimed at developers who are well versed with progrmming but are trying to start with embedded platform in general and Arduino in particular. Choice of Arduino is obvious because of it’s simplicity which makes Arduino learning real simple. For most of the connections, I have preferred connecting the hardware with Pin connectors and bread boards so that those who are not comfortable with Soldering and PCBs can find it easy to follow.Stage 4 Complete Beginner's Guide For Arduino Hardware Platform For DIY

2. Setting up Arduino Development Environment

First download latest Arduino IDE from Arduini’s Official Software Download Site. Windows user will be able to download an Installer. The installer has a FTDI usb driver along with the IDE itself. Prefer to install the USB driver. This is essential. Without USB driver your IDE can not communicate with the Arduino Device.
Following is the Pin Description of the device that I am using.
Once the software is installed, plug Arduino’s USB cable to your laptop. You will see a notification in tray “installing driver”. Once driver installation is successfull Open Device manager ( start->right click on Computer icon->Properties->Device manager).
In the Ports(Com and LPT) section you see USB serial port. In case of Arduino Uno R3, you will see Arduino Uno marker along the port. When you plug out the cable, you won’t find the port anymore. So this is the port at which your Arduino device is connected.
If you have connected your device for the first time, you will also notice that the LED near digital pin 13 is blinking. Most of the Arduino boards are preloaded with a blink program which blinks the LED connected to pin 13.
Before starting with our coding, we need just couple of more steps to setup IDE with the board.
Open the IDE. Tools->Boards select the board you are connecting as shown in figure below.
Once the board is selected, you need to set the correct serial port as you have figured out from figure 2.2
Before we go into programming the boards we will learn some simple basics of Arduino C programming.

2.2 Basics of Arduino C Program

Following is the basic structure of the Arduino program. An Arduino C program is also known as a sketch.

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:
}

setup() is executed once ( when you reset the device, or restrat the device, or plug in the USB cable ). loop()
runs continuesly. Main logic must be implemented inside loop.
Arduino Duemilanova/Diecimilia/Uno R3 ( from here onwards will refer simply as Arduino) board has 14 Digital IP pins. Each pin can be program separately. A Pin can be in with OUTPUT mode or input mode which needs to be specified in setup using
pinMode(PIN_NO,OUTPUT) or pinMode(PIN_NO,INPUT); 
Input mode is used when you connect a switch to a pin and want the pin to read the status. These digital pins in input mode can only recognize logic level input like 0 and 1. PIN_NO ranges from 0-13 depending upon the pin you are programming.
you can write logical output in a pin in OUTPUT mode or can read the the value at the pin ( 1 or 0) usingdigitalWrite(PIN_NO,HIGH)/digitalWrite(PIN_NO,LOW) and int n=digitalRead(PIN_NO)respectively.
Few pins in Digital ports are marked as PWM( like 10 and 11 in figure). They can be used to generate gate pulse of different duty cycle. PWm pin can be programmed to produce perfect square gate pulse of specific duty cycles using analogWrite(PIN_NO,byteValue) Where byteValue can be betweet 0 to 255 where 255 represts highest or 100% duty cycle.  PWM pins can be used to control DC motors precise speed control or controlling intensity of electrical bulbs through MOSFETs/IGBTs and so on.
Arduino has 6 analog read pins (0 to 5) opposite to the digital ports. These are input pins which are connected to 10 bit ADC. Sensors can be directly connected to these pins. Arduino can read the voltage at the pin with intvalue=analogRead(ANALOG_PIN_NO) 
The software comes with a Serial library, through which other programs can communicate with Arduino using Serial communication. Arduino IDE also comes with a Serial Monitor through which one can test the serial communication.
For initializing serial communication, in the setup function one needs to initialize the serial communication object with Serial.begin(BAUD_RATE) Baud Rate varies in the multiples of 96200.Serial.print("msg"),Serial.println("msg")Serial.write(someVariable) are common functions by means of which the board can write data on the serial port which can be read by any external program capable of communicating with the device over serial port.
int v=Serial.read() is used to read data from Serial port written by other programs on serial port.
delay(delayInMilli); is used to make the next instruction wait for delayinMilli period of time which is specified in milliseconds.
Most of the basic hardware Input and output can be performed using above commands.
I request you to please go through My Arduino Getting Started Tutorial more more information about Arduino C programming and Serial Communication basics.

3. Arduino Power Management

3.1 Powering Up Arduino Board

An embedded system may need to drive several loads like motors, trigger relays, provide powers for sensors and so on. Therefore understanding how much power the device draws and how much power it is capable to provide to provide to other peripheral hardware unit is very important aspect of embedded system. In this section we will learn about power management of Arduino.
Arduino’s operating voltage is 5v as with most of the embedded system devices and boards. It can be powered up either through a USB  or through an External power source. Arduino board has a voltage regulator that can produce 5v regulated supply to the board. External power source’s voltage is regulated through this. As with any voltage regulator, the unit needs higher voltage input to produce 5v output. Hence when Arduino is to be powered by external voltage source like battery, it has to be a 9v power supply. Figure 3.1 Shows different ways to power up Arduino board.
You can use a 9v Adapter to convert AC to 9V DC directly and give input to Arduino through it’s power jack. If you want the device to be completely battery driven, you can use a Connector for 9V battery and can put that in power port. Arduino also provides two pins by label Vin and Gnd  which just preceeds the analog ports. You can connect your 9V battery with Arduino using these two pins with the help of battery connecting wires.
However you have a Jumper in the board just near the USB plug which needs to be set to USB if you are using USB to power up Arduino or in Vin if you are using external power. Jumper is shown in figure  3.2.

3.2 Output Power of Arduino

When you look at Arduino board straight as in figure 3.1, you can see a set of pins grouped togather which are RST, 3V, 5V, GND, GND, Vin.  I call the pins togather as Power Port. The 5v and 3v pins are sometimes also referred to as VCC pins as they are source to other devices. For example if you want to connect a sensor with Arduino ( form example LM 35), it needs 5 v to operate. This voltage is provided by 5v pin. Devices like ZigBee operates at 3V. Such devices can be powered up by 3v VCC pin. Current drawing capability of Vcc pins are 200mA.  Arduino also has 14 digital IO pins. When provided with HIGH logic level, these pins produces 5v output. However current drawing capability of digital IO pins are as low as 40mA Due to very low current drawing capability these pins can not drive any load. Even LED is difficult to drive with this current. Hence digital pins can be used to control other devices only through a switching electronic device like an Optocoupler ( MCT2E) or a transistor (Digital Switching of Devices will be explained later).
It is important to note that digital pins suopports reading voltage just like controlling devices. When a digital switch is connected to digital I/O pins whose’s logic level needs to be read by the pins, the pin is said to be acting like a Sink. Care should be taken such that sum of current being sinked to the device through all I/O pins must not exceed 100mA. hence it is always advisable to connect a switch output to digital pin with current limiting resistor.
The maximum current that can be supplied to any pins is about 40mA. So if you connect a 5v-1A power supply to any of the analog pins, even though the voltage is within the range, over current will literally fry your microcontroller. Therefore when you work with Arduino, ample care must be taken to understand the voltage and current specification of the devices and limit input current whenever you use any pins ( analog/digital) as Sink by connecting the source through appropriate current limiting resistor.
From the above discussion we derive following important points:
1) Arduino 5v, 3v Vcc pins can be used directly to drive low current loads( like LED, LCD, 5V DC motors, Sensors)
2) Arduino digital pins are not sufficient to drive even low current loads
3) Arduino digital pins can be used to provide 5V switching voltage to digital switches. Higher current rating devices can be switched by Arduino using a coupler like Optocoupler or Transistor.
4) Arduino pins as Sink has very low current specifications. Therefore high current source must not be connected to any of the pins configured as sink. Use current limiting resistor with the pins as Sink.
With this knowledge we are literally ready to move ahead with our Arduino development.

4. Indicators And Switches

Often in your DIY projects you need indicators. Say for instance when you build a fire alarm system, you sensor may monitor heat and light intensity and when the value exceeds threshold value, you want it to trigger an alarm.
You may also want to indicate that a specific device is active through a LED. LEDs and Buzzers are most common indicators that are part of almost all DIY projects. Because of the ease of use, cost effectiveness, hardware developers are fond of these two components.
Switches plays an important role for triggering an external event for the device. For example you want a motor to start: press a switch, let the board read the event and turn the motor on. You want to turn on a light by pressing a switch:  use simple switches.
So indicators and switches are most basic, common and most widely used components with any hardware projects. They are cheap, they are efficient and they are effective. Hence in this section we will discuss about using using LEDs and Switches with Arduino.

4.1 LED

4.1.1 Working With Onboard LED

Light Emitting Diode or LED’s are most common indicators for any electronic devices from an adapter to laptop. It makes it visible for the user that certain part of the system is functioning well. LEDs are available in different colors and current specification. Current specification of LEDs varies from 30mA to several amperes.  Because LEDs draws current, it is also treated as current load.  LEDs can operate between 1.2V to about 24v  depending upon it’s specification.
Arduino board has a built in LED which can be controlled by Pin 13 of Digital pins. In order to control any device through Arduino digital IO pins, we must configure the pin as OUTPUT pin and make it high using digitalWrite(13,HIGH).
Simple Sketch for blinking can be found in Arduino example: File->Examples->Basics->Blink. For continuity of the discussion, I am simply dumping the code here.

Hide   Copy Code
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

This will make your LED associated with pin 13 switching on and off in turns in every 1 sec.  Turned on state of the LED can be seen in figure 4.1.
Figure 4.1: LED Connected to Pin 13

1.2 Connecting and Controlling External LED

The Onboard LED is fine, it needs no circuit connections. But what about connecting an external LED and controlling it with digital pins. As digital pins may not provide enough current for LED, we need the LED to be drawing current from a source which can supply enough supply. So we will connect the Anode of the LED ( the longer leg) with Vcc 5v of Arduino. But if we connect the cathod ( smaller leg) directly to ground, firstly the we can not control the LED as we can not control Vcc pin. So we connect LED’s cathod to Emittor of an NPN transistor ( I have used BC 548, you can use any other transistor of NPN type). Transistor’s collector is grounded. Though there is no resistor with LED in 4.2 a), I advise you to connect the LED with BC548 with a 100Ohm resistor. It limits the current and prevents the LED being fried up.
Transistor’s base is connected to any of Arduino’s digital i/o pin ( say pin 12) through a bais resistor R2(100/470 ohm). When the pin is HIGH, transistor gets base voltage which completes transistor circuit and LED is grounded through the transistor. When the pin is LOW, transistor does not get forward bais voltage so is turned off. Thus LED’s ground pin is not connected to LED. Hence it is turned off.
You can actually take a 3 pin connector, put the transistor in it and connect with it’s wires. You can also make the connection in breadboard if you so wish!
You can now use the code listing of 4.1 to perform the experiement. Just change Pin 13 to pin 12. Alternatively you can also add pin 12 as digital output pin and control external LED with on board LED. 4.2 b) Shows glowing of LED.

4.2 Buzzer

Just like LED, Buzzer is also an output component. There are many different types of buzzer out of which Pizeo electric buzzers are most common. But unlike LEDs buzzer’s current requirement is very low. It can operate in current as low as 10mA. Therefore we can connect the buzzer directly with a digital pin and Arduino’s ground.
You can connect the buzzer with Pin 8 ( Try to avoid a pin which is marked as PWM) and done. Use the code listing of 4.1 to turn on and off the buzzer in alternative seconds. Do not forget to change the pin number to the buzzer pin you have connected the buzzer with.
Buzzer will either come with two wires or two pins. If the buzzer has two pins, the longer leg is anode and must be connected to the digital pin and the shorter leg is cathod which must be connected to ground.
If the buzzer has wires ( as in figure 4.4) then RED wire is positive and blue/black wire is ground. Figure 4.4 Shows the Buzzer Connection with Arduino.

4.3 Switches

Needless to say that there are several Switches available. The one we would experiement with is a Push button or Push Switch. It remains on till you push the button and becomes off when you leave it. You would switch on a LED with push button click.
Push switches have four legs, which provides two pair f switching poles. Figure 4.5 b) shows how the pins are paired. So while switchin a single device with a switch ( which we generally will do), select either pair A with B or C with D. Note A , C and B,D are interchangable.
Circuit diagram of the Switch with LED is shown in Figure 4.5 A). LED is given positive voltage through Arduino 5v through a resistor. The cathod is connected to one pole of the switch and another pole of the same pair is connected to ground. So when switch is pushed, LED’s circuit is complete and it glows. When switch is released, LED’s circuit is broken and it is turned off.
We do not need any coding for this experiement.  The result can be seen from figure 4.6

5. Arduino With Display

LCD’s are most common output devices for embedded system. From Beverage Vending machine to handheld ticket printer to hotel billing system, you see the display with many embedded system. There are several displays which are commonly used. Most common displays are: LCD Displays, Graphic Displays, Seven Segment Displays and many more. Duel Line LCD displays are most common device that is used with embedded system. That is because embedded system functionalities are generally restricted. So 2 Line display is sufficient to display most of the things.
Here I am going to show you how to work with display in Arduino. LCD is a 16 pin device which operates at 5V. Arduino’s 5V Vcc is sufficient to power LCDs. However LCDs don’t come with pin headers which makes it difficult to work without Soldering.
However we have a way out for every such problem. Look at figure 5.1 c). This is a standard Multi pin Connector. 8-Multi pin Connectors are available in themarket for about $.5-$1. You can get two 8-pin connectors and put the male pins in the LCD and the respective female socket’s pre soldered wires go to Arduino pin.
LCD’s needs two VCCs: One for lighting up front characters and another one is for backlight which is optional. Figure 5.1 A) Shows the Connection Table. LCD pins 11,12,13,14 are the data pins which should be connected to Arduino’s digital I/Os. RW‘  should be grounded for enabling the write operation. Enable and RS are two control pins.
Therefore from Arduino, we just need to control 6 pins. VO controls the brightness. You might want to vary it depending upon environment light. So connect it with a 10K variable resistor called pot to ground. POT’s first and last pin must be connected to 5 v and ground respectively. The middle one goes to VO pin of LCD.
Once connection is done, we can take the help of LiquidCrystal library of Arduino to print in the LCD.

Hide   Copy Code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2); //create the lcd variable
void setup() 
{
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  lcd.clear();                    //clear the LCD during setup
  lcd.begin(16,2);                //define the columns (16) and rows (2)
}
void loop() {
  lcd.print("Codeproject.com");
  lcd.setCursor(0,1);            
  lcd.print("Rupam, Gulbarga");          //print...
  delay(2000);
  lcd.clear();                   //clear LCD, since we are still on 2nd line...
  lcd.home();                    //set the cursor the top left
}

 LiquidCrystal lcd(12,11,5,4,3,2); is most important here. lcd is a variable of LiquidCrystal class which is initialized with (RS,ENABLE,D4,D3,D2,D1). Where all all the values are respective Arduino Pin Numbers and not LCD pin numbers.
lcd.begin(NO_OF_CHARACTERS,NO_OF_LINES) initialize the lcd for printing NO_OF_LINES and NO_OF_CHARACTERS par line. lcd.home() takes the cursor to first character of first line. lcd.print() can print characters starting from a position where the cursor is currently in.
lcd.clear() clears all data from LCD. lcd.setCursor(CHARACTER_NO,LINE_NUMBER) takes the cursor to particular LINE_NUMBER and CHARECTER_NO of that specific Line.
Figure 5.2 Shows the output of above listing with Circuit diagram specified in 5.2 A). You are free to change the pins. But do not forget to change lcd initialization accordingly.

6. Arduino With Sensors

6.1 Sensor Principles

Sensors are small electronic devices that can convert physical parameters to electrical parameters. There are wide range of sensors for measuring environment parameters like Humidity, Temperature, Moisture, Co2 and many more. Figure 6.1 Shows different sensors that might be used with Arduino.
Sensors generally have a operating voltage and current range. Most of the common sensors operates at input 5v and requires medium range current. Which means that sensors can be supplied with voltage directly from Arduino’s 5V Vcc pin.
Sensors produces electrical output which is in the range of operating voltage.  Arduino can read this voltage through it’s Analog pins and there are 6 analog pins grouped togather as right most port at the bottom. The operating voltage range of these pins are 5V and operating current is about 100mA. One of the important thing you must know is that if you do not connect any sensors to any of the analog pins, the pins would still not read zero as expected. That is because they tend to pick up environmental charge.Thus in open state, reading the pins will return random low voltage.

6.2 A/D Converter

As operating voltage is 5v, most of the sensors works with 5v. However as electrical parameters are analog ( continues) in nature, so is the sensor output.
But Microcontrollers care capable of processing only digital data. Therefore Arduino provides a on board A/D ( Analog to Digital Converter) of 10 bits. A/D converter works with the logic of sampling and quantization. It samples the signal with number of bits equal to A/D bits. Then quantize the input signal to nearest sample value.
You can read this Wiki on A/D converter for a better understanding of the concept.
The meaning is that Max voltage i.e. 5V can be represented by 2^10 i.e. 1024. So if the value returned by say analogRead(5) is x, then actual voltage =
Sensor output Voltage=x*5/1024
Further output voltage of sensor will be associated with some value of physical parameters. For example temperature sensor LM35 which we will shortly see produces output voltage in the range of 0-1.1V for temperature from 0-92’C respectively. Thus 1.1/92 ( or about .1v) represents 1 unit of temperature. This is also called sensor precision.
As sensors output voltage or current according to the physical parameter it is monitoring, they are also called sources and Arduino ( or any other device reading the sensor data) is called a sink. When many sensors are connected through wireless technique and many sinks are network togather with a common objective ( for instance measuring average temperature of an area) it is called Wireless Sensor Network or WSN.

6.3 Temperature Sensor

I have already provided the details about temperature and light sensor in my Live Weather Station with Arduino‘s tutorial. However I am writing this particular article for absolute beginners. Therefore this and next subsection will be based on the concepts presented in the aforementioned tutorial. There will be no change in the connections. However we will differ in code. In this particular tutorial I will not be using ArdOS the way I did it in the previous tutorial.
We will begin with configuring our hardware and testing it first. We would follow it by integrating the setup with Internet f Things.
We will begin with Temperature sensor LM 35. This is possibly one of the cheapest and effective sensors to kick start with your Arduino DIY. It is a three pin sensor which operates between 0-5V range and used for measuing temperature in the range of 0-92’c.
If you are a hardware beginner and just starting to learn your hardware then it is always a good idea to study the data sheet of the components before working with them. It gives you electrical and logical understanding essential for working with those devices.
You can download LM35 Datasheet from here.
LM35 has three pins, which are to be connected to Ground, Vcc and Analog pin of Arduino. You can provide it power directly from Arduino 5V pin and connect the output pin to any of the analog pins. I have connected it to pin 5. See figure 2.1 to understand the circuit.Stage 4 Complete Beginner's Guide For Arduino Hardware Platform For DIY Schematic
Figure 6.2: LM 35 Connection With Arduino
You might wonder how to connect those three pins to Arduino.Plenty of tutorial will suggest you to use bread board. So you can either use bread board or use three pin connector. I prefer short three pin connector as shown in figure 2.2. So you insert the LM35 pins into the three slots of the connector and insert the wires into respective Arduino slots.
Figure 6.3: Connecting LM 35 using 3-Pin Connector
Now before we code LM 35, a little understanding of Concept isn important.
LM35 Converts temperature to voltage of range 1V. That means When Temperature is about 90’c, output voltage of LM35 will be about 1V. Now how Arduino reads this temperature?
As you have noticed the output voltage of LM35 is connected with Arduino’s Analog pin 5, Arduino will read the analog voltage which will be converted to digital value through internal 10 bit ADC. 10Bit ADC means that Arduino reads 1024 (2^10) when voltage is 5 volt. But as output of LM35 will never exceed 1V, 4Vs or about 10% of the precision is lost. This can be overcome simply by changing the reference voltage from 5V to 1.1V. This can be achived using following simple code.

 Collapse | Copy Code
Hide   Copy Code
analogReference(INTERNAL);

However, if you use 1.1V as aRef, the equation changes entirely. If you divide 1.1V over 1024, each step up in the analog reading is equal to approximately 0.001074V = 1.0742 mV. If 10mV is equal to 1 degree Celcius, 10 / 1.0742 = ~9.31. So, for every change of 9.31 in the analog reading, there is one degree of temperature change.
Therefore,
Temperature value= Value_Read_By_Pin_5/9.31 with 1.1 reference voltage. Let us write a simple program to probe the temperature once in every Second.
Here goes our code for the same:

 

For more detail: Stage 4: Complete Beginner’s Guide For Arduino Hardware Platform For DIY


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top