MM 221 Sensors and Measurement Lab
Lab Report 07 - 11/09/2024
Prajwal Talwalkar 23B2456
Tarachand Sharma 23B2465
Objectives of Lab 7:- Learning coding and interfacing with Arduino
ACTIVITY-1: (a) Creating and downloading sketches to Arduino
UNO. ‘Blink a light’:
Narration:- In this activity, we checked the blinking feature of Arduino board
Observations:-
Learnings:- We learned that after the successful performance of the blink test, the
Arduino board worked perfectly fine.
b) Blinking externally connected LED:
Narration:- In this activity, we assembled the hardware using the electrical components
as shown in the circuit. We modified the code to blink LEDs on Pins 2 and 7 alternately
every 0.5 seconds, then compiled, uploaded and ran the code successfully.
Circuit Used:-
Observations:-
[Link]
aring
Learnings:- We learned how to get output at selected pins and how to control the output
simultaneously as well.
Activity-2) Interfacing a Passive beeper with Arduino:
Narration:- In this activity, we connected the passive beeper to the Arduino, as shown in
the circuit. We modified the code from Activity-1 by adding the necessary lines for PWM
control using `analogWrite()`. After adding the lines to the header and setup function,
we inserted the code to toggle the output value in the main loop. We compiled,
uploaded, and ran the modified code.
Circuit Used:-
Observations:-
[Link]
aring
Learnings:- We learned about the functions of the beeper and how voltage difference
creates the sound of the beep. We also noticed the variations in the code for the new
modified circuit.
Activity-3) Generating Morse-Code with the Arduino Board:
Narration:- In this activity, we connected the beeper to the Arduino, as shown in the
circuit. After compiling and uploading the code, the beeper started producing the Morse
code for the phrase 'IIT Bombay.' We then updated the code to output the Morse code
for our name.
Circuit Used:-
Observations:-
[Link]
aring
Learnings:- We learned about how the morse code works and values for alphabets and
how to check for the pattern while listening to the beep.
Activity-4: Reading the ON/OFF status of a push-button(switch)
input:
Narration:- In this activity, we connected three wires to the board as shown in the circuit:
two for the 5V supply and ground, and one from digital pin 2 to the pushbutton, which is
connected through a 10k ohm pull-down resistor to ground. After uploading the code,
we pressed the push-button and confirmed that the onboard LED 'L' reflected its status.
We then connected a beeper to Pin-5 and modified the code to sound the beeper and
light the LED 'L' whenever the button is pressed. The code was also updated to send
the push-button status to the serial monitor, indicating “0” for released (OFF) and “1” for
pressed (ON).
Circuit Used:-
Observations:-
[Link]
ring
Learnings: We learned about how to give input to Arduino and how to manipulate them
to get the desired outcome. The push button acts as our input and the LED on the
arduino boards acts as the output. We also learned about the function of the push
button and how the circuit inside is.
Activity-5: Reading Analog Input voltage:
Narration:- In this activity, we inserted a potentiometer into the breadboard and wired it
as shown in the circuit. After uploading the code, we opened the serial monitor to see
the voltage levels change as we rotated the potentiometer. The values reflected the
analog input conversion. Next, we connected an LED to Pin-9 and uploaded the
relevant code. As we turned the potentiometer, we observed the changes in LED
intensity based on its position. We took a screenshot of the serial monitor and saved the
code for future use. After that, we ran the Serial Plotter to see a graphical display of the
analog output voltage variations.
Circuit Used:-
Code:
/*
Analog input, serial output
Reads an analog input pin
Also prints the results to the Serial Monitor.
The circuit:
- potentiometer connected to analog pin A0.
Center pin of the potentiometer goes to the A0 pin.
side pins of the potentiometer go to +5V and ground
This code has been written for the MM221 Lab
*/
// These constants won't change. They're used to give names to the pins
used:
const int analogInPin = A0; // Analog input pin that the potentiometer is
attached to
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the
sensor
void setup() {
// initialize serial communications at 9600 bps:
[Link](9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// print the results to the Serial plotter
[Link](sensorValue);
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
sensorValue = analogRead(analogInPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
delay(30);
}
Observations:-
Learnings:- We learned about how the potentiometer works and how to use the variable
resistance values for our purpose. We also learned the function of an analog-to-digital
converter, which converts the input voltage into numbers from 0 to 1024. We also
figured out how to analyse the graphs in the arduino software.
Activity-6 : Understanding Pulse-width-modulation (PWM) technique used by the
AnalogWrite() command :
Objectives:
From the Arduino IDE Menu, select File> Examples> [Link]> [Link]
Between lines 40 and 41, insert a single line – outputValue = 127; 4.
Record the voltage between Pin-9 and GND pin with the help of a multimeter. Also
observe the waveform.
Code:
const int analogInPin = A0; // Analog input pin that the potentiometer is
attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached
to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
[Link](9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
outputValue = 192;
analogWrite(analogOutPin, outputValue);
// print the results to the Serial Monitor:
[Link]("sensor = ");
[Link](sensorValue);
[Link]("\t output = ");
[Link](outputValue);
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
}
Learning:- In this activity, we learned about how to generate functions from arduino. In
this activity, we generated a square wave-form type wave and changed efficiency to our
wish.
Activity-7: Using the AnalogOut command to
drive an RGB LED:
Objective:
The command analogWrite(redPin, 0) switches OFF the red LED and analog
Write(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.
Results:
Learnings:-
In this activity, we learned how to control the voltage for many pins all at once and we
changed the LED colour by changing the voltage input from the pins. Also, considering
the resistance in all cases was the same the manipulation was easy.
Activity-8: Measuring room temperature
with the LM35 temperature sensor.
Objective:
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.
Connect Vout (pin-2) of the LM35 sensor to the A0 terminal of the Arduino
board.
Using bottom we did this experiment
Learnings: We learned about using the LM-35 temperature sensor and how the wind
speed affects the temperature reading values. We also learned about how the transition
of thermal energy to electrical energy happens.
Activity-9: Increasing the precision of
measurement of sensor output.
Objective:
The temperature dependent voltage output of LM35 shows some fluctuations.
In this activity, we modified the code developed for Activity-8 so that 500 concurrent
measurements are averaged before displaying on the serial monitor.
Code:
const int analogInPin = A0; // Analog input pin that the potentiometer is
attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached
to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
[Link](9600);
}
void loop() {
// read the analog in value:
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);
}
Initial value = 27 deg C
Final value = 31 deg C
63.2 of initial value = {27 + (31-27)*(0.632)}
= 29.528 deg C
Time constant = 325 m sec (approx.)
Learnings:- In this activity, we learned about the ADS device and how to use its library
in the Arduino software. We also figured out how to minimize the noise generated from
ADC as it is a gaussian distribution.
Bonus Activity-1: Interfacing an external
Analog to Digital Converter with Arduino
board for the purpose of data
acquisition.
Objective:
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.
In this experiment, we had performed following steps:
Step-1: Inter-connect the Arduino board and ADS115 ADC board.
Step-2: Install the ADS1115 library.
Step-3: Measure and record data. (It is results)
Step-4: Assemble the circuit shown on the breadboard and connect to Arduino pins +5V
and GND. Connect A1 – A3 shown in figure to the A1-A3 Pins on the ADS1115 board.
Code:
#include<ADS1115_WE.h>
#include<Wire.h>
#define I2C_ADDRESS 0x48
/* There are several ways to create your ADS1115_WE object:
* ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48
* ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS
* ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object
/ I2C Address = 0x48
* ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together
*/
ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS);
void setup() {
[Link]();
[Link](9600);
if(![Link]()){
[Link]("ADS1115 not connected!");
}
/* Set the voltage range of the ADC to adjust the gain
* Please note that you must not apply more than VDD + 0.3V to the input
pins!
*
* ADS1115_RANGE_6144 -> +/- 6144 mV
* ADS1115_RANGE_4096 -> +/- 4096 mV
* ADS1115_RANGE_2048 -> +/- 2048 mV (default)
* ADS1115_RANGE_1024 -> +/- 1024 mV
* ADS1115_RANGE_0512 -> +/- 512 mV
* ADS1115_RANGE_0256 -> +/- 256 mV
*/
adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change
parameter to change range
/* Set the inputs to be compared
*
* ADS1115_COMP_0_1 -> compares 0 with 1 (default)
* ADS1115_COMP_0_3 -> compares 0 with 3
* ADS1115_COMP_1_3 -> compares 1 with 3
* ADS1115_COMP_2_3 -> compares 2 with 3
* ADS1115_COMP_0_GND -> compares 0 with GND
* ADS1115_COMP_1_GND -> compares 1 with GND
* ADS1115_COMP_2_GND -> compares 2 with GND
* ADS1115_COMP_3_GND -> compares 3 with GND
*/
[Link](ADS1115_COMP_0_GND); //comment line/change
parameter to change channel
/* Set number of conversions after which the alert pin asserts
* - or you can disable the alert
*
* ADS1115_ASSERT_AFTER_1 -> after 1 conversion
* ADS1115_ASSERT_AFTER_2 -> after 2 conversions
* ADS1115_ASSERT_AFTER_4 -> after 4 conversions
* ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default)
*/
//[Link](ADS1115_ASSERT_AFTER_1); //uncomment if you want
to change the default
/* Set the conversion rate in SPS (samples per second)
* Options should be self-explaining:
*
* ADS1115_8_SPS
* ADS1115_16_SPS
* ADS1115_32_SPS
* ADS1115_64_SPS
* ADS1115_128_SPS (default)
* ADS1115_250_SPS
* ADS1115_475_SPS
* ADS1115_860_SPS
*/
// [Link](ADS1115_8_SPS); //uncomment if you want to change the
default
/* Set continuous or single shot mode:
*
* ADS1115_CONTINUOUS -> continuous mode
* ADS1115_SINGLE -> single shot mode (default)
*/
[Link](ADS1115_CONTINUOUS); //comment line/change parameter
to change mode
/* Choose maximum limit or maximum and minimum alert limit (window) in
Volt - alert pin will
* assert when measured values are beyond the maximum limit or outside
the window
* Upper limit first: setAlertLimit_V(MODE, maximum, minimum)
* In max limit mode the minimum value is the limit where the alert pin
assertion will be
* cleared (if not latched)
*
* ADS1115_MAX_LIMIT
* ADS1115_WINDOW
*
*/
//adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5); //uncomment
if you want to change the default
/* Enable or disable latch. If latch is enabled the alert pin will
assert until the
* conversion register is read (getResult functions). If disabled the
alert pin assertion will be
* cleared with next value within limits.
*
* ADS1115_LATCH_DISABLED (default)
* ADS1115_LATCH_ENABLED
*/
//[Link](ADS1115_LATCH_ENABLED); //uncomment if you want to
change the default
/* Sets the alert pin polarity if active:
*
* ADS1115_ACT_LOW -> active low (default)
* ADS1115_ACT_HIGH -> active high
*/
//[Link](ADS1115_ACT_LOW); //uncomment if you want to change
the default
/* With this function the alert pin will assert, when a conversion is
ready.
* In order to deactivate, use the setAlertLimit_V function
*/
//[Link](); //uncomment if you want to change
the default
[Link]("ADS1115 Example Sketch - Continuous Mode");
[Link]("All values in volts");
[Link]();
}
/* If you change the compare channels you can immediately read values
from the conversion
* register, although they might belong to the former channel if no
precautions are taken.
* It takes about the time needed for two conversions to get the correct
data. In single
* shot mode you can use the isBusy() function to wait for data from the
new channel. This
* does not work in continuous mode.
* To solve this issue the library adds a delay after change of channels
if you are in contunuous
* mode. The length of the delay is adjusted to the conversion rate. But
be aware that the output
* rate will be much lower that the conversion rate if you change
channels frequently.
*/
void loop() {
float voltage = 0.0;
adc.setVoltageRange_mV(ADS1115_RANGE_2048);
[Link]("0: ");
voltage = readChannel(ADS1115_COMP_0_GND);
[Link](voltage);
adc.setVoltageRange_mV(ADS1115_RANGE_1024);
[Link](", 1: ");
voltage = readChannel(ADS1115_COMP_1_GND);
[Link](voltage);
adc.setVoltageRange_mV(ADS1115_RANGE_2048);
[Link](", 2: ");
voltage = readChannel(ADS1115_COMP_2_GND);
[Link](voltage);
adc.setVoltageRange_mV(ADS1115_RANGE_0512);
[Link](", 3: ");
voltage = readChannel(ADS1115_COMP_3_GND);
[Link](voltage);
delay(1000);
}
float readChannel(ADS1115_MUX channel) {
float voltage = 0.0;
[Link](channel);
voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt
return voltage;
}
Learnings: in this activity, we learned about how to measure voltage through the
ADS and then to the arduino board. The 4 readings shown are of 4 different resistors
setup and their voltage difference.
Bonus Activity-2: Coding the cigarette
butt riddle
Objective:
Butt-found and a cigarette-assembled events shall be indicated by LEDs & beeps of
varying length.
Interface an RGB LED, Push-button and a passive beeper.
This experiment required [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.
Code:
// Constants declaration
const int analogInPin = A0; // Analog input pin that the
potentiometer is attached to
const int redLed = 3;
// Analog output pin that the Red LED of
the RGB is attached to
// Analog output pin that the Green LED
const int greenLed = 6;
of the RGB is attached to
const int blueLed = 9;
the RGB is attached to
const int buttonPin = 2;
// Analog output pin that the Blue LED of
// The number of the push-button pin
const int analogOutPin = 5; // Analog output pin for the beeper
const int maxbutts = 16;
// Total number of cigarette butts found
before ending
int outputValue = 127;
beeper sound
int buttcounter = 0;
// Value to generate 50% PWM for the
// Variable to count the butts found
int cigarettecounter = 0; // Variable to count the cigarettes made
void setup() {
// Initialize LED pins as output
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
// Initialize the push-button pin as input
pinMode(buttonPin, INPUT);
// Initialize serial communications at 9600 bps
[Link](9600);
}
void loop() {
// Wait for the push-button press and process butt found event
if (digitalRead(buttonPin) == HIGH) {
buttcounter++;
// Blink Red LED and beep for 0.25s for each butt found
digitalWrite(redLed, HIGH);
analogWrite(analogOutPin, outputValue);
delay(250); // 0.25 second delay
digitalWrite(redLed, LOW);
analogWrite(analogOutPin, 0);
delay(250); // Short pause
// Serial message for butt found
[Link]("Butt No ");
[Link](buttcounter);
[Link](" found");
// If enough butts to make a cigarette
if (buttcounter % 8 == 0) {
cigarettecounter++;
// Turn on Green LED and beep for 1 second
digitalWrite(greenLed, HIGH);
analogWrite(analogOutPin, outputValue);
delay(1000); // 1 second delay
digitalWrite(greenLed, LOW);
analogWrite(analogOutPin, 0);
// Serial message for cigarette made
[Link]("Cigarette number ");
[Link](cigarettecounter);
[Link](" smoked");
}
delay(1000); // 1 second delay after each butt event
// If 16 butts have been processed
if (buttcounter == maxbutts) {
endRiddle();
}
}
}
void endRiddle() {
// Serial message for end of riddle
[Link]("END OF RIDDLE");
// Beep and blink Blue LED in pattern to indicate end of riddle
for (int i = 0; i < 5; i++) {
digitalWrite(blueLed, HIGH);
analogWrite(analogOutPin, outputValue);
delay(250); // Beep and blink for 0.25s
digitalWrite(blueLed, LOW);
analogWrite(analogOutPin, 0);
delay(220); // 0.22 second silence
}
// Stop the loop after the riddle ends
while (true) {
// Do nothing, the sequence is complete
}
}
Result:
[Link]
aring