0% found this document useful (0 votes)
23 views17 pages

Arduino UNO Coding and Interfacing Guide

The document provides an overview of Arduino, an open-source electronics platform designed for creating interactive projects using hardware and software. It covers the basics of Arduino hardware, including the Arduino UNO, digital and analog I/O pins, and the Integrated Development Environment (IDE) used for programming. Additionally, it outlines several lab activities involving coding, interfacing with components like LEDs and beepers, and generating Morse code using Arduino.

Uploaded by

varkanwill
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views17 pages

Arduino UNO Coding and Interfacing Guide

The document provides an overview of Arduino, an open-source electronics platform designed for creating interactive projects using hardware and software. It covers the basics of Arduino hardware, including the Arduino UNO, digital and analog I/O pins, and the Integrated Development Environment (IDE) used for programming. Additionally, it outlines several lab activities involving coding, interfacing with components like LEDs and beepers, and generating Morse code using Arduino.

Uploaded by

varkanwill
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MM221 Lab-6: Learning coding and interfacing with Arduino

• What is Arduino
Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino
boards can read switches, keys and buttons, switch-on lights, read sensors, activate relays, motors, valves,
GPS units, cameras and integrate with IOT (internet of things) and publish real-time status online.
• The Arduino hardware (Fig-1) and software were designed for artists, designers, hobbyists, hackers,
newbies, and anyone interested in creating
interactive objects or environments. This
flexibility combined with the fact that the
Arduino software is free, the hardware boards
are cheap, and both the software and hardware
are easy to learn has led to a large community
of users who have contributed code and
released instructions for a huge variety of
Arduino-based projects. In this lab we use the
most used version of Arduino namely Arduino
UNO. The heart of the Arduino board is the
microcontroller ATmega328P microcontroller
from Atmel.
Fig-1: Image of the official Arduino board
Microprocessors and Microcontrollers
Typical microprocessors & microcontrollers used in embedded systems have following architecture

Microprocessor system Microcontroller

CPU: Central processing unit Int: Interrupt lines


ALU: Arithmetic logic unit BUS: Set of pins for interfacing memory and external
GPR: General purpose registers peripherals
I/O: Digital Input and Output lines Peripherals: Timers, Counters, Direct Memory access
peripherals, digital, analog and serial input/output ICs.
• What is meant by digital I/O
A microcontroller can read the status of a switch (On or Off) by monitoring the voltage at some of its
designated pins
Such pins are termed digital input pins since these read the logical status (0/1) of voltage presented at the
pins where logical ‘0’ is represented by voltage in the range of 0 - 0.8V, and logical ‘1’ is represented by
voltage in the range 2.5-5V.
Often, a switch is wired in such a manner that it produces a logical 0 voltage when it is closed and a logical
1 voltage when open
In Arduino digital output pins have been provided to switch on Lamps, valves, motors etc. in a similar
manner. To switch ON a lamp, logical ‘1’ is output on the digital output pin. Likewise logical ‘0’ is output for
switching OFF the lamp
External electronic circuits use the logical signals to switch ON/OFF actual loads.
In Arduino, Pins 1-13 (Fig-3) are called digital I/O pins as these can be used as either logical input or logical
output pins. Pins marked as A0-A5 (Fig-3) can read voltages in the range of 0-5V with the help of internal
Analog to digital converter (ADC).

• The process of creating and uploading the code on Arduino


The Arduino Integrated Development Environment (fig-2) - or Arduino Software usually referred as IDE (for
Integrated Development Environment) - contains i) a text editor for writing code, ii) a message area, iii) a
text console, iv) a toolbar with buttons for common functions and v) a series of menus. The IDE provides
tools to connect to the Arduino hardware to upload programs and communicate with them.
The IDE also provides a terminal screen which can be used to send and receive data from the computer.
Sketches:
Programs written using Arduino Software (IDE) are called sketches. These sketches are written in the text
editor and are saved with the file extension. ino. The editor provides features for cutting/pasting and for
searching/replacing text. The message area gives feedback while saving, exporting and displays errors. The
console displays text output by the Arduino Software (IDE), including complete error messages and other
information. The bottom righthand corner of the window displays the configured board and serial port. The
toolbar buttons allow you to verify and upload programs, create, open, and save sketches, and open the
serial monitor.
1

3 4 2 5 6

Fig-2: 1) The menu bar and (2-6) Important shortcuts; 2) Create new file, 3) Compile, 4) Upload on Arduino,
5) Open an existing file, 6) Save file.
7 8
• Functional details of Arduino UNO. 9
10

13
1
12

11

14

5 4 3 6
Fig-3: Description of various resources on the Arduino Uno board
o Power source ((1) USB or alternatively (2) Barrel Jack for DC voltage input (5-18V)
o Pins (5V, 3.3V, GND, Analog, Digital, PWM, AREF)
GND (3): Short for ‘Ground’. There are several GND pins on the Arduino, any of which can be used to
ground your circuit. 5V (4) & 3.3V (5): As you might guess, the 5V pin supplies 5 volts of power, and
the 3.3V pin supplies 3.3 volts of power. Most of the simple components used with the Arduino run
happily off 5 or 3.3 volts.
o Analog (6): The area of pins under the ‘Analog In’ label (A0 through A5 on the UNO) are Analog Input
pins. These pins can read the signal from an analog sensor (like a temperature sensor) and convert it
into a digital value that we can read.
o Digital (7): Across from the analog pins are the digital pins (0 through 13 on the UNO). These pins can
be used for both digital input (like telling if a button is pushed) and digital output (like powering LED).
o PWM (8): You may have noticed the tilde (~) next to some of the digital pins (3, 5, 6, 9, 10, and 11 on
the UNO). These pins act as normal digital pins, but can also be used for something called Pulse-Width
Modulation (PWM). We have an activities for PWM generation as a part of this lab, but for now, think
of these pins as being able to simulate analog output (like fading an LED in and out).
o AREF (9): Stands for Analog Reference. Most of the time you can leave this pin alone. It is sometimes
used to set an external reference voltage (between 0 and 5 Volts) as the upper limit for the analog
input pins.
o Reset button (10).
o Power LED indicator (11). This LED remains ON as long as the power is applied to the Arduino board
o TX RX LEDs (12) indicate that a serial communication is in progress
o A general-purpose LED ‘marked L (13) which shall be used in the very first Arduino program.
o Main microcontroller IC (14)
• Analog input peripherals in Arduino
The microprocessor used in the Arduino board has a 10 bit Analog to Digital Converter (ADC) which
provides six analog input channels which can accept voltage in the range of 0-5V.
The Arduino ADC divides the 5V range into 1024 steps (210) whereby the input voltage on the range o 0-5V
is represented by number in the range 0-1023.

o Analog output peripherals in Arduino


The microprocessor on the Arduino board does not have the classical Digital to Analog converter (DAC)
peripheral. However, Arduino board does provide an 8bit digital output by employing the PWM technique
explained in Fig-8
A number in the range of 0-255 is converted by the Arduino to a proportional DC voltage in the range of 0-
5V.

• Connecting the Arduino board to a computer


For connecting the Arduino board to the computer, one requires-
• Arduino Uno Board
• USB-B to USB-A Cable
• Window 10/11, macOS or Linux OS
• Arduino IDE (Software for compiling the
Arduino code)
• Connect the Arduino board to the
Laptop/PC
• Setup/install the drivers (This is
automatic in Windows)
• Identify the com port used by Arduino
• Run Arduino IDE
• Set the correct com port and board
• Write the code (code written for the Arduino board is often called a sketch)
• Compile and download the sketch.
• Prerequisites for the Arduino lab: Installing and using the Arduino IDE
• Arduino software (version 2.3.2 ) can be downloaded from the moodle course page (for the windows
OS) or from [Link] for any of the operating systems. You may use the video
tutorial [Link] as a guide. Mac users are advised to view
[Link]
• Your first code: Watch the YouTube tutorial
o [Link]
o [Link]
o Upon executing the Blink LED program, if all goes well, the LED marked ‘L’ tagged as #13 in figure-
3 shall start blinking at 1sec frequency.
In case of issues, go through the following check-list.
1. Have you chosen the board as Arduino Uno? If not, select this board from the menu: Tools> Boards>
2. Check whether correct serial port has been selected. If not, select port from menu: Tools> Port>
In case multiple ports are shown, if in doubt, call your TA/instructor.
3. Are you able to compile the code? In case of errors, troubleshoot the code. Take help from TAs/instructor
if necessary.
4. If the code compiles successfully, are you able to download the code? If not, check
i) if correct port is selected.
ii) For Win11 PCs, download the latest Win11 Arduino driver from Moodle and install
iii) If issues remain unresolved, call your TAs/instructor
5. If the code compiled and downloaded, but the program does not run as expected, take help from your
TA/instructor.
ACTIVITY-1: (a) Creating and downloading sketches to Arduino UNO. ‘Blink a light’:
Blinking LED: From the Arduino IDE Menu: Select File> Examples> [Link]> [Link]
In the IDE, edit the Lines-34 & 36 from delay (1000); to delay (250);
Compile, download and run the code. Observe that the LED ‘L’ (Figure-3, #13) starts flashing.
Carefully read the sketch. Note that Pin-13 of the Uno board is internally connected to the on-board LED
marked ‘L’. Pin 13 has also been brought out as digital output among other pins provided by Arduino-Uno.
b) Blinking externally connected LED: Now, let us use some of the other digital output pins.
Assemble the hardware shown in Fig-4. Additional hardware required: LEDs, 1K resistors, connecting wires
and a breadboard

Pin5 Pin3

Longer leg is
Anode
Fig-4: Connecting external LEDs. Observe that each LED is connected through a series resistance
Examine the previous sketch and modify it to interface with LEDs on Pins 2 & 7. Also change the code
to blink each LED alternatively for 0.5 seconds.
Compile, upload, and run the modified code. Make a short video for the lab journal.

Activity-2) Interfacing a Passive beeper with Arduino:


Passive beeper is a simple piezoelectric element. When excited with a pulsating waveform, it vibrates
and produces a sound. The PWM output 1 (read foot note) feature of the analog output pin shall be used
to generate the pulsating waveform. PWM is also explained in detail in Activity-6 and Fig-8.
Connect the beeper with the Arduino between the pins shown in the Figure-5. Change the code
developed for activity-1 by using the analogWrite() command.
Add following lines of code just above the setup()
function,
const int analogOutPin = 5;
int outputvalue;
and the following line of code inside the setup()
function
pinMode(analogOutPin,OUTPUT);
Write following lines of code in the main loop
outputvalue=127;
analogWrite(analogOutPin, outputvalue); Fig-5: Interfacing a beeper with Arduino
delay(500);
outputvalue=0;
analogWrite(analogOutPin, outputvalue);
delay(500);
Compile, upload, and run the modified code. Take help from TAs in case of difficulty. Take a snapshot
of the setup. Record a short video and show output to your TA before moving to next example.
Activity-3) Generating Morse-Code with the Arduino Board: (Use the circuit given in Fig-5)
• Morse code is based on tones (between 500-900Hz) of varying duration characterized as a dot
(length= 1 unit time) and a Dash (length = 3-unit time).
• Each alphabet is coded with a unique combination of 1 to 5 dots and/or dashes.
• The tone is generated by using the PWM generation feature of the Arduino board. Bursts of
980Hz PWM generated on Pin-5 (and 500Hz on pin-9) of the Arduino board can be used to create
the dot and Dash sounds.
• Download from moodle, the file [Link] and place it at the path
…/Documents/Arduino/moresecode/. ……….[These instructions pertain to the Windows OS]
• Compile and upload the code on the Arduino board.
• Connect a beeper with the Arduino between the pins shown in the Figure-5. The beeper should
start producing the Morse code for the phrase ‘IIT Bombay’.
• Examine and understand the [Link] file. Update the code file to output Morse code of
your name. Record a short video. Show output to your TA before moving to the next activity.

1
PWM (Pulse Width Modulation) is used by Arduino to produce a variable Analog voltage from Digital output pins. PWM is a
𝑂𝑁 𝑡𝑖𝑚𝑒
repetitive pulse waveform having a fixed amplitude but a variable duty cycle [𝐷𝑢𝑡𝑦 𝑐𝑦𝑐𝑙𝑒 = ] . Effective analog
𝑂𝑁 𝑡𝑖𝑚𝑒+𝑂𝐹𝐹 𝑡𝑖𝑚𝑒
output voltage = 5.0*(duty cycle). Arduino provides 0-100% duty cycle for PWM value in the range of 0-255 (0:255::0:100%). Thus,
choosing the PWM output value of 127 allows a near square wave output. For more details see Activity-6.
Note that Pins-3, 9, 10 & 11 generate PWM frequency of 490Hz. For higher output frequency (980Hz) use Pin 5 or pin6.
Activity-4: Reading the ON/OFF status of a push-button(switch) input:
In this exercise, we shall read the status of a key-switch (read ‘0’ for open and ‘1’ for closed switch).

Connect three wires to the board as detailed in Fig-6. The first two, red and black, connect to the two long
vertical rows on the side of the breadboard to provide access to the 5Volt supply and ground. The third wire
goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down
resistor (here 10k ohm) to ground. The other leg of the button connects to the 5-volt supply.
Pushbuttons or switches connect two points in a circuit when you press them. When the pushbutton is open
(not-pressed) there is no connection between the two legs of the pushbutton, so pin-2 is connected to ground
(through the pull-down resistor) and reads as LOW, or 0. When the button is closed (pressed), it makes a
connection between its two legs, connecting the pin-2 to 5 volts, so that the pin reads as HIGH, or 1.
If you disconnect the digital I/O pin from everything, its reading may change erratically. This is because the
input is "floating" - that is, it does not have a solid connection to voltage or ground, and it will randomly return
either HIGH or LOW. That is why you need a pull-down resistor in the circuit.

Push button switch

+5V

Gnd

10K

Fig-6(a): Interfacing a key switch Fig-6(b): Connection details

• From the Arduino IDE Menu: Select File> Examples> 02 > Digital> Button>
• Compile and upload the code on the Arduino board.
• Press the push-button. Observe that the onboard LED ‘L’ now reflects the status of the push-button.
• Connect a beeper between Pin-5 and ground. Modify code to sound beeper and light the onboard LED ‘L’
whenever the push-button is pressed.
• Carefully read the code example: Examples> [Link]> AnalogReadSerial> to understand how the serial
monitor is used.
• Update the code to carry out three activities namely,
1. LED ‘L’ reflects the status of push-button.
2. Beeper sounds whenever the push-button is pressed.
3. Sends the status of the push-button to the serial monitor (Button-released (OFF) = “0” and Button-
pressed (ON) = “1”).

Complete this activity. Take snapshots for ON & OFF state of the Push-button for your lab journal.
Activity-5: Reading Analog Input voltage:

Just like the Arduino is used to send digital output (e.g. light a LED) and read digital input (e.g. read a
push-button), it can also be used to source analog voltage (analog-output) and measure analog voltage
(analog-input) (this activity) from sensors and other voltage sources.
1. Insert a small 5K – 10K potentiometer (R1) in the breadboard and wire as follows: (Fig-7a).

Get +5V from the Arduino


Pin-9
board2 R

1 1K

LED
3 Gnd
1 2 3
Figure-7a: Circuit for Analog input Fig-7b: Connect a LED on Pin9
2. Download the code file [Link] from Moodle and place it at the path
…/Documents/Arduino/SerialAnalogInput /. ……[These instructions pertain to the Windows OS]
3. Compile the file in Arduino IDE and download this code to the Arduino board.
4. From the Arduino menu, run the serial monitor as follows. From the Menu: Tools> Serial Monitor>
5. Rotate the potentiometer knob to obtain various voltage levels, which after being converted by the
Arduino board is sent to the serial monitor. The serial monitor displays this data received after each
interval as a new line-item. Go through the code to understand serial output commands.
6. Observe that the measured value lies between 0-1023. Thus, the onboard ADC converts the incoming
voltage (0-5V) into 1024 steps (since 210 = 1024, this is called 10-bit Analog to Digital Conversion (ADC)).
Note that the 100 protection resistor in series with the Pot limits the ADC count to ~965.
7. Open the Examples file from Arduino IDE Menu: File> Examples> [Link]> [Link]
8. Connect a LED at Pin-9 as shown in Fig-7b. Compile, upload, and run this Arduino code file.
9. Rotate the Potentiometer R1 in Figure-7 and observe changes in ADC data displayed as before.
10. This piece of code scales the read voltage range from 10bits (0-1023) to 8 bits (0-255). Note that this
value is sent as Analog Output voltage (0-5V) to Pin-9 of Arduino board which also has the LED.
11. Therefore, the LED intensity changes as the potentiometer is rotated from one end to the other.
12. Go through the code carefully to understand the usage of various Arduino commands.
13. Set potentiometer midway. Take a screenshot of the serial monitor and include in your journal.
14. Save this code file. This code shall be used again for Activity-8
15. Close the serial Monitor and run the Serial Plotter as follows: Menu: Tools> Serial Plotter>
16. Rotate the pot CW and CCW to obtain a graphical display of the variations of analog output voltage. Read
the changing data on the serial plotter screen.
17. Take a screenshot of the serial plotter screen and report in the e-journal.
18. Open the Examples file from Arduino IDE Menu: File> Examples> [Link]> AnalogInput
19. Compile download and run the code. See the effect of Potentiometer on blinking of the LED ‘L’.
Activity-6: Understanding Pulse-width-modulation (PWM) technique used by the AnalogWrite() command:
1. Arduino can output an analog voltage using a technique called Pulse width modulation (PWM). Watch
the 20min Video Tutorial. [Link]
2. From the Arduino IDE Menu, select File> Examples> [Link]> [Link]
3. Between lines 40 and 41, insert a single line – outputValue = 127;
4. Compile, download and run the code. Record the voltage between Pin-9 and GND pin with the help of a
multimeter. Also observe waveform at Pin-9 on the CRO. You will observe a near square wave.
5. Keep the scope connected. Change the previously inserted line to outputValue=254; Compile, download
and observe the waveform. Record the voltage at Pin -9 with a multimeter.
6. Repeat this for outputValue=63 and 32; . Report the results (PWM wave and multimeter voltage)
7. Do you find a relation between the outputValue parameter and the voltage at Pin-9?
8. You will observe that the width of the rectangular waveform at Pin-9 depends upon the ouputValue sent
to the specific Pin.
9. Specifically, in the Arduino code, outputValue parameter range of 0 to 255 is mapped on the average
output value range of 0 to 5V. Thus, a count of 63 produces 1.25V, count of 127 produces 2.5V, count of
191 produces 3.75V while the count of 255 produces 5.0Volts.
10. Figure-8 shows the effect of various PWM output waveforms and their average DC value
11. Use DSO to record PWM on Pin-9 at three outputValue parameters namely 64, 128 and 192. Take
snapshot of the DSO screen for each case. Add the snapshots to the lab journal.

Pulse Period
(T)
Pulse Width
(t) OutValue count = 255/4= 63
5V

1.25V
t t
Duty cycle = 25%
𝐏𝐮𝐥𝐬𝐞 𝐰𝐢𝐝𝐭𝐡 𝐭 OutValue count = 255/4= 127
𝐝𝐮𝐭𝐲𝐜𝐲𝐜𝐥𝐞 = =
𝐏𝐮𝐥𝐬𝐞 𝐩𝐞𝐫𝐢𝐨𝐝 𝐓
5V
2.5V
t t
Duty cycle = 50% OutValue count = 255/4= 190

5V
3.75V

t t
Duty cycle = 75%
PWM signal Average voltage of the PWM output

Figure-8: PWM signal and corresponding Average value seen on the multimeter. Note that the PWM signal is
𝑃𝑢𝑙𝑠𝑒 𝑊𝑖𝑑𝑡ℎ 𝑂𝑁 𝑇𝑖𝑚𝑒
characterized by its duty cycle where duty cycle = 𝑃𝑢𝑙𝑠𝑒 𝑃𝑒𝑟𝑖𝑜𝑑 = 𝑂𝑛 𝑇𝑖𝑚𝑒+𝑂𝐹𝐹 𝑇𝑖𝑚𝑒
Activity-7: Using the AnalogOut command to drive an RGB LED:
1. Download the RGB_LED_Drive.ino file from moodle and place it in the folder
…/Documents/Arduino/RGB_LED_Drive/ . ……….[These instructions pertain to the Windows OS]
2. Open the file in Arduino IDE, compile and download on the Arduino board.
3. Connect a common cathode RGB LED as shown in figure-9.

Figure-9 : RGB LED drive by Arduino


Go through the code and understand its operation. Salient points are-
4. The command analogWrite(redPin, 0) switches OFF the red LED and analogWrite(redPin, 255)
switches ON the red LED. Here 0 corresponds to 0V output on the respective Pin while 255
corresponds to 5.0Volts on the respective Pin.
5. Run the code and observe the change in colors.
6. Make changes to the code to generate color sequences of your interest. Take a short video.

Activity-8: Measuring room temperature with the LM35 temperature sensor.


• LM35 is a 3-terminal semiconductor temperature sensor which
generates voltage proportional to temperature in °C. The package
style, pin details and Arduino connectivity is shown in Figure-10.
• Connect the pins 1 and 3 of LM35 sensor as shown in Figure-10.
• Connect Vout (pin-2) of the LM35 sensor to the A0 terminal of the
Arduino board.
A0 GND

1
2 To A0

3
Gnd

Figure-10
• Test with the program with File> Examples> [Link]> [Link]
• Use the code “[Link] on moodle [analogRead() function] to read the sensor voltage.
• Modify code to convert sensor voltage to the Temperature and display on the serial monitor.
• To check whether the system is functioning, measure your body temperature by holding the sensor
tightly in your thumb and index finger. You should see an increase in the sensor output voltage.
• Using the LM35 output characteristic, namely 10mv = 1°C, modify the program to display
temperature instead of displaying the mV value. Take snapshot of Temperature display for journal.
𝟓𝟎𝟎𝟎𝒎𝒗 𝑺𝒆𝒏𝒔𝒐𝒓 𝒗𝒐𝒍𝒕𝒔(𝒎𝒗)
𝑺𝒆𝒏𝒔𝒐𝒓 𝒗𝒐𝒍𝒕𝒔(𝒎𝒗) = 𝑨𝑫𝑪 𝒄𝒐𝒖𝒏𝒕 ∗ 𝐚𝐧𝐝 𝑻𝒆𝒎𝒑𝒆𝒓𝒂𝒕𝒖𝒓𝒆(℃) = 𝒎𝒗
𝟏𝟎𝟐𝟒 𝟏𝟎 ( ℃ )

Activity-9: Increasing the precision of measurement of sensor output.


• Use the hardware assembled for Activity-8
• Observe that the temperature dependent voltage output of LM35 shows some fluctuations.
• Such fluctuations are due to the internal ADC noise of the microprocessor on the Arduino board
• The ADC (Analog to Digital Converter) noise usually has a gaussian distribution. It should
therefore be possible to reduce or altogether eliminate the ADC noise by integrating the signal
over a reasonable length of time. This can be done by averaging several concurrent ADC
measurements.
• In this activity, modify the code developed for Activity-8 so that 500 concurrent measurements
are averaged before displaying on the serial monitor.
• Use following code-snippet as a guide to implement the averaging of the sensor voltage. Insert
this code in the main loop. Read, understand, and change the averaging to 1000 concurrent
measurements.

float sum=0; //define a variable which shall be used during averaging


//The FOR loop makes 'i' measurements and accumulates these into the variable 'SUM'
for (int i=1;i<=500;i++) {
sensorValue = analogRead(A0);
sum = sum + sensorValue ;
delay(1);
}
//Compute average of the accumulated sensor value by taking average of ‘SUM’
sensorValue = sum/500.0;
//Scale the ADC count to temperature
sensorValue=sensorValue*(5000.0/1024.0)*(1.0/10.0) ; //mv/10
// print the results on the Serial Monitor
[Link] ("Temperature = ");
[Link] (sensorValue);
[Link] (" °C");
delay (1);

Allow the LM35 sensor to cool for 5min(imp). Now change to Serial Plotter (Menu>Tools> Serial Plottor> )
Tightly hold the sensor in your thumb and index finger. Wait for 5-6min till the display stabilizes. Take a
snapshot of PC screen and determine the time constant of the sensor. Time constant is the time taken by the
sensor from the instant that the temperature starts increasing to the time taken to reach 63.2% of its final
value. Include the snapshot and the time constant calculations in the Lab journal.

Activity-10: Interfacing an external Analog to Digital Converter with Arduino board for the
purpose of data acquisition.

Background: Computers and microprocessors represent Analog voltages by converting them into a digital
representation format. This is achieved by a process called Analog to digital Conversion implemented by devices
called Analog to Digital converters (ADC).

Analog to Digital conversion involves

1. Defining the range of voltage which needs to be represented (e.g. 0 - 5V or -5.0 to +5.0V), and,
2. Dividing this voltage range into a finite number of steps (e.g., 2000, 20000, or 2 10, 212, 216 etc..).

A 3 ½ digital multi-meter is the best example of an ADC which converts an input range (e.g. 0-2.0V) into 2000
steps where each step represents 1.0mV. Likewise, a 4½ multimeter converts the same input range into 20000
steps such that each step represents 0.1mV. Thus a 20000 step ADC has better resolution than the 2000 steps
ADC. ADC converters typically specify resolution in 2n steps where ‘n’ is usually in the range of 8 - 16. Special
high-resolution ADC may have n ranging from 18 to 32.

The Arduino board has a built-in ADC which has resolution of 210 or 1024 steps. If higher ADC resolution is
required, this can be obtained by interfacing Arduino with external ADC chips.

In this activity, we shall interface a Texas Instruments 16bit ADC (216) ADS1115 chip. The ADS1115 ADC chip has
a Programmable Gain amplifier at the input and it can convert input voltages
from four independent channels. Figure-11 shows a board designed for
interfacing the ADS1115 chip with the Arduino board. The function of each of
the ADS1115 interface Pins is described below.

A0 – A4 Four independent analog input channels (Input range 0-5V)


VDD Positive terminal of the power supply (Up to 5VDC)
GND Ground terminal of the power supply
SCL & SDA These are the control lines using the I2C protocol. Thes pins
are used to interface the ADS1115 chip with the CPU.
ADDR Pin sets the chip address of the ADS1115 chip
ALRT Pin is used by the ADS1115 chip to Alert the CPU after an
ADC conversion is complete

Pre-requisites: Collect the following items before starting this experiment

1) Arduino board
2) ADS1115 board Figure-11
3) Bread-board
4) Assorted wires and resistors
5) Arduino IDE and [Link] software should be installed on the PC.
Step-1: Inter-connect the Arduino board and ADS115 ADC board as show in the figure-12.

A0
A1
A2
A3

Figure-12: Use above figures as a guide for connections between Arduino board and ADS1115
Step-2: Install the ADS1115 library.

• Run the Arduino IDE. From the menu bar navigate – Menu: Sketch> Include library> Manage Libraries>
• The Library Manager window shall open. In the search box, enter ads1115_we and press enter. From
the displayed list, click at Install button shown in the ADS1115_WE box. The library should install
automatically. The library package also includes several example files.
Step-3: If not already done, assemble circuit to test interfacing of the ADS1115 board, run the test ‘Example’
program

• Plug in the Arduino board to the PC. Select board as Arduino Uno and appropriate port for the board.
• Insert ADS1115 on a breadboard and interconnect with Arduino board as detailed in Figures 12 & 13.

Figure-13

• From the menu bar, navigate – Menu: File> Examples> ADS1115_WE> [Link] and press enter.
• The example code file shall open. Compile the code and upload to the Arduino board.
Step-4: Measure and record data

• Note that while Figure-12 shows that ADS1115 can take input from four Analog channels A0-A3, we
have connected only one channel (A0) to the 3.3V source on the Arduino board.
• Open the Serial Monitor. Observe that the voltage measured from all the four input channels is
displayed each line item.
• Since channel A0 of the ADS1115 board is connected to the 3.3V source on the Arduino board, observe
the highlighted column in Figure-14 which has a value close to 3.3V.

Figure-14
• Take snapshot of the assembled circuit on the breadboard. Include the snapshot as well as the output from
the Serial monitor display in your lab journal

Step-5: Assemble the circuit shown in Figure-15 on the breadboard and connect to Arduino pins +5V and GND.
Connect A1 – A3 shown in Figure-15 to the A1-A3 Pins on the ADS1115 board.

22K

From the
Arduino board

150 Ohms

Step-6: Add the lines of code highlighted in red boxes below (Figure-16) at appropriate places in the main loop
of the code. Compile and upload the modified code.
4096

Figure-16
• Run the Serial monitor to view measured voltages. Confirm that approximately following voltage values are
displayed on the four ADS1115 channels.
o A0 = 3.3V
o A1 = 0.65V
o A3  1.56V
o A4 = 0.16V
• Take snapshot of the assembled circuit on the breadboard. Include the snapshot as well as the output from
the Serial monitor display in your lab journal

Bonus Activity: Coding the cigarette butt riddle

The riddle: A person can make a full-length cigarette out of 4 cigarette butts. After he makes a cigarette
out of the cigarette butts, he smokes it. During a long walk, he found 16 cigarette butts lying on the road.
What is the maximum number of cigarettes he could have smoked?

Solution:

• Initially, full cigarette can only be assembled after finding 4 individual butts.
• After smoking the first cigarette, the person has a leftover cigarette butt.
• Now only 3 additional butts are required to assemble a new cigarette.

Riddle implementation on Arduino.

• Butt-found and a cigarette-assembled events shall be indicated by LEDs & beeps of varying length.
• This activity uses interfaces created in three prior activities, namely activities 2, 4 and 7.
• Download from moodle and watch the “Cigarette butt riddle video”

External interface:

• Interface an RGB LED, Push-button and a passive beeper as shown in Fig.-17 and 18.
• Download file [Link] from moodle.
• This code checks the hardware as follows: Waits for a key-press. Once the Push-button is pressed, RGB
Led blinks (and the beeper sounds) in the following sequence: Red→ Green→ Blue.

Functional requirements of the software:

• A push-button is to be used for signaling the cigarette-butt found event. Each cigarette-butt found
event should generate a short beep of 0.25s and a single blink of Red LED (0.25s ON and then OFF).
This should also send a message on serial monitor “Butt No <n> found” followed by 1 sec delay.
• Whenever adequate number of butts are collected to make a full cigarette, switch ON the green LED
and sound a single 1-second-long beep after which the green LED should switch OFF. This should also
send a message on the serial monitor: “Cigarette number <m> smoked”.
• Continue this sequence for 16 key presses. Stop the sequence after 16 keypresses (16 butts-found).
• After the last butt has been processed, indicate the end of the riddle by generating a beeping sound
(0.25s beep, 0.22s silence) along with blinking of Blue LED. Serial Monitor output: “END OF RIDDLE”.
• Carefully go through figure-17 before assembling the circuit.
• Show full-operation to the TA. Make a short video of the entire operation and include in lab journal.
PIN USAGE:
RED LED: Pin 9
GREEN LED: Pin 6
BLUE LED: Pin 3
KEY: Pin 2
BEEPER: Pin 5
Ground
3
6
9

ARDUINO 2

5
3

+5V

Fig-17: Interfacing external components for bonus activity

Fig-18

Common questions

Powered by AI

Key components include the Arduino board, the ADS1115 board, a breadboard, and assorted wires and resistors. To interface, not only are hardware setups required, but installing the ADS1115 library in the Arduino IDE is essential. The ADS1115 offers advantages such as a higher resolution (16-bit), multiple input channels (up to 4), and a programmable gain amplifier, allowing more precise voltage readings and supporting input from sensors with higher sensitivity than the onboard ADC .

Incorrect duty cycle values in analogWrite() can lead to suboptimal functioning of PWM-based applications. For LEDs, miscalculated duty cycles can result in unexpected brightness levels, reducing power efficiency or altering visual feedback. For motors, improper duty cycles might cause jerky movements or reduced speed control precision, impacting performance or leading to mechanical stress. Precise control of duty cycles ensures that PWM applications perform optimally within their designed operational parameters .

PWM allows the Arduino to simulate an analog output by rapidly switching a digital signal on and off. By changing the duration of the on state (the pulse width) relative to the off state, an average voltage is produced proportional to the duty cycle. The pulse width can represent analog values by using a digital pin and the analogWrite() function, which adjusts this range from 0 to 255 to generate a proportional voltage between 0 and 5V .

To modify the Blink example for controlling an external LED, you would edit the sketch to include digital pins 2 and 7 for output. The code written to toggle the internal LED connected to pin 13 would be adapted to account for the external connections. This involves changing the pin numbers in the sketch and using the digitalWrite() function to toggle the state of each pin. The delay function could also be adjusted to set the desired blink rate, like using delay(500) for a 0.5-second blink interval per pin .

The Arduino IDE (Integrated Development Environment) helps in writing, compiling, and uploading code (called sketches) to Arduino boards. It acts as a bridge between the programmer and the microcontroller, offering a user-friendly interface to select board configurations, manage libraries, and utilize example sketches, thus streamlining the development process for Arduino projects .

External ADCs like ADS1115 enhance data acquisition by providing higher resolution (16-bit versus Arduino's 10-bit), which allows for finer granularity in data representation over the input range. It supports additional features such as differential inputs and a programmable gain amplifier, making it suitable for precise measurement applications and interfacing with sensors that output lower voltages or require higher sensitivity .

Before uploading a sketch, ensure that the correct board (Arduino Uno) and COM port are selected. The right drivers should be installed, especially on Windows 11, and the latest Arduino IDE version should be used. Compiling the code without errors is crucial; otherwise, troubleshooting with TAs or instructors is advised. It's important to select the exact serial port as not all listed will handle the board, especially for functioning code uploads .

The 'blink LED' program introduces beginners to fundamental concepts of programming a microcontroller, including setting pin modes, controlling digital outputs, and utilizing delays to create timing controls. It underscores the hardware-software interaction, helping learners get familiar with writing, compiling, and uploading sketches while observing immediate physical outputs (LED blink) connected directly with the program logic .

Serial output allows data from the Arduino to be sent to the serial monitor, providing real-time insights and debugging capabilities. Observing the serial output helps verify whether the expected ADC values are shown, identifying discrepancies, and assisting in fine-tuning the program or hardware setup. By continuously monitoring changes, such as those occurring with a potentiometer, it can display dynamic changes in analog data and assist in logical decision-making in experiments .

The AREF pin allows users to set an external reference voltage for the analog input pins, effectively setting the upper limit for analog value measurements. This is useful when operating with sensors that output voltages over a different range, allowing the full resolution of the ADC to be applied to a smaller, more precise voltage range .

You might also like