Download IOT Practical 8 write-up TE AIDS SPPU 2024 Pattern and more Study Guides, Projects, Research Internet and Information Access in PDF only on Docsity!
PRACTICAL EXPERIMENT NO. 3
Title : Collection of real-time temperature data using an IoT sensor and application of a machine learning model to predict future temperature values. Problem Statement: Implement an IoT application to collect real-time temperature data using an IoT sensor and apply a machine learning model to predict future temperature values.(Any Machine Learning model). Aim: Write a program(IoT application) to collect real-time temperature data using an IoT sensor and apply a machine learning model to predict future temperature values using any Machine Learning model. Objectives:
- To study and understand Collection of real-time data using an IoT sensor.
- To study and understand various machine learning models.
- To study and understand application of a machine learning model to predict future values. Prerequisites:
- Knowledge of C,C++ and Python Programming.
- Knowledge of Arduino IDE/ESP-IDE/Raspbian, LEDs, Sensors. Outcome: An application(program) which transmits sensor data between an IoT device and a server using communication protocol. Software requirements: 64 - bit Open Source Operating System (Windows/Ubuntu), ardOS/Arduino IDE, Raspbian OS/Thonny IDE, ESP-IDE Hardware requirements: Arduino Uno/ ESP module / Raspberry Pi Board and Sensors. Theory: Rapberry Pi GPIO
Raspberry Pi GPIO (General Purpose Input/Output) pins are physical connectors on the board that let the Pi interact with the outside world. The standard 40-pin header allows you to send/receive signals to components like LEDs, relays, and sensors using programming languages such as Python. These are programmable pins on the Raspberry Pi that allow you to send (output) or receive (input) signals. The GPIO pins can be found on the so-called header of the Raspberry Pi, usually a row of 40 pins (26 in older models). In addition to GPIO pins, this header also contains special connections. For example: power (3.3V, 5V and GND) and communication protocols (I2C, SPI and UART). The very first Raspberry Pi, the Model A Rev 1.0, marks the beginning of the Raspberry Pi era. This compact model has a 26-pin GPIO header. The GPIO pins use an older numbering than newer models. GPIO0 on pin 3 also functions as SDA for I2C. GPIO1 on pin 5 also functions as SCL. Other important pins are GPIO4 on pin 7 and GPIO14 and GPIO15 on pins 8 and 10. The latter two are used for UART communication, for sending and receiving respectively. Also, GPIO17 through GPIO25 can be found spread across pins 11 through 22 and pin 24. For SPI communication, GPIO10, GPIO9, GPIO11, GPIO8, and GPIO7 are important, each mapped to a specific pin from 19 through 26. The power pins provide 3.3V on pin 1, 5V on pins 2 and 4, and ground via pins 6, 9, 14, 20 and 25. These provide power or ground to connected components. The Raspberry Pi 2, 3 and 4 all use a 40-pin GPIO header. They share the same physical pinout. This allows you to use HATs and expansion boards on either model without any modifications. The underlying hardware is different, but the GPIO layout remains largely the same. The Raspberry Pi Zero and Zero 2 W are small, power-efficient and versatile. They have a full 40-pin GPIO header, allowing you to use the same accessories, HATs and expansion boards as larger models such as the Pi 3 and Pi 4. The pinout layout is identical to that of the Pi 2, 3 and 4. So you have access to standard GPIO pins, power sources and communication protocols such as I2C, SPI and UART. PWM and clock signals are also available. The 40 pins are divided over two rows of 20. They provide access to:
- Power pins: 5V on pins 2 and 4, 3.3V on pins 1 and 17, and multiple GND pins.
- Standard GPIOs: Freely programmable, usable as input or output.
- Communication protocols: o I2C: pin 3 (SDA) and pin 5 (SCL) o SPI: pin 19, 21, 23, 24 and 26 o UART: pin 8 (TXD) and pin 10 (RXD) The Raspberry Pi 5, like previous models since the Pi B+ and Pi 2, uses a 40-pin GPIO header. This keeps many HATs and expansion modules compatible. However, the Pi 5 does bring some technical improvements. The header has a standard 2x20 pin layout: two rows of twenty. The pinout is largely the same as that of the Raspberry Pi 4. Under the hood, however, there are clear updates. For example, a new I/O controller provides faster GPIO interfaces. The first two pins on the top left provide 3.3V and 5V power. After that comes a mix of GPIOs, ground pins, and communication pins. Think SDA/SCL (I2C), TX/RX (UART), and MOSI/MISO/SCLK/CE (SPI).
Input/Output: The most basic, but also most used function of GPIO pins is that of digital input or output. In this mode, pins can be programmed to read (input) or write (output) signals.
- Input : In this mode, a GPIO pin 'listens' for an electrical signal. For example, you can use this to read a button. When the button is pressed and voltage passes through, the signal on the pin changes from low (0) to high (1), or vice versa.
- Output : In this mode, the GPIO pin 'sends' a signal to a component. For example, think of controlling an LED, a relay or a buzzer. The pin then sets the signal to high (3.3V) or low (0V) to activate something. The Raspberry Pi GPIO pins operate at 3.3 volts of logic , meaning a high output voltage is 3.3V. Caution : Applying 5V signals directly to a GPIO pin can permanently damage the Pi. In this case, use a voltage divider or logic level shifter. Power pins (3.3V, 5V, GND): In addition to the programmable GPIOs, the Raspberry Pi header also contains a number of dedicated power pins. These are intended to supply power to external components such as sensors, modules or microcontrollers.
- 3.3V pins o Pins: 1 and 17 o These pins provide a constant 3.3V voltage directly from the Pi's voltage regulator. They are suitable for powering smaller 3.3V components.
- 5V pins o Pins: 2 and 4 o These connect directly to the Pi's 5V power supply (via USB or GPIO power input) and can be used to drive more powerful modules that require 5V.
- GND (Ground) o Multiple pins including 6, 9, 14, 20, 25, 30, 34 and 39 o Ground is the reference point for voltage and is essential in any electrical circuit. All connected components must be connected to at least one GND pin to function properly. Correctly using the power pins is crucial for the stability of your project. Overloading the 3.3V regulator or shorting the 5V line can damage the Raspberry Pi.
I2C (SDA/SCL):
I2C (Inter-Integrated Circuit) is a serial communication standard. It allows multiple devices to connect using just two data lines with the Raspberry Pi. I2C is ideal for sensors, displays, and low-bandwidth chips. The two lines are:
- SDA (Data) – Pin 3 (GPIO2)
- SCL (Clock) – Pin 5 (GPIO3) These lines together form a bus. Both the Raspberry Pi and other devices are connected to this bus. Each device gets a unique address. This allows multiple components to work on the same two pins at the same time. To use I2C, you need to enable it via raspi-config or config.txt. Pull-up resistors are needed — usually present on modules. If not, use e.g. 4.7kΩ to 3.3V. SPI (MOSI/MISO/CLK/CE): SPI (Serial Peripheral Interface) is a faster communication bus than I2C and is often used when speed or reliability is important. Unlike I2C, SPI requires more data lines. The standard SPI bus on the Raspberry Pi consists of the following pins:
- MOSI (Master Out Slave In) – Pin 19 (GPIO10)
- MISO (Master In Slave Out) – Pin 21 (GPIO9)
- SCLK (Clock) – Pin 23 (GPIO11)
- CE0 (Chip Enable 0) – Pin 24 (GPIO8)
- CE1 (Chip Enable 1) – Pin 26 (GPIO7) The Pi acts as a master and sends data via MOSI to the connected slave device. That device may send data back via MISO. CE0 and CE1 are used to indicate which device the Pi is currently communicating with (so you can connect multiple SPI devices as long as each uses its own CE pin). SPI is ideally suited for applications such as:
- Fast ADC/DAC modules
- TFT and OLED screens
- SD cards
- RFID readers As with I2C, SPI must first be enabled in the OS via raspi-config. It is also important to consider 3.3V logic on the Pi when connecting devices that operate at 5V. UART (TX/RX): UART (Universal Asynchronous Receiver/Transmitter) is a simple way to send data between two devices. Think of communication between a Raspberry Pi and a microcontroller, GPS module or sensor. The Raspberry Pi has standard UART on these pins:
- TXD (transmit): Pin 8 (GPIO14)
- RXD (Receive): Pin 10 (GPIO15) With UART, you use only two lines to send and receive data. Because the protocol is asynchronous, both devices must set the same baud rate. Common values are 9600 or 115200 bits per second. By default, the Raspberry Pi uses UART for console signals. If you want to use UART for your own projects, you need to disable this feature via raspi-config or config.txt. Pay attention to the voltage level. The Pi operates on 3.3V logic. Connecting a device that uses 5V can cause damage. PWM (pulse width modulation): PWM (Pulse Width Modulation) is a method of modulating a digital signal to behave like an analog signal. The Raspberry Pi doesn’t have built-in analog outputs, but with PWM you can still do things like:
- Dimming an LED steplessly
- Controlling the speed of a motor
- Controlling the position of a servo By default, two pins are available on the GPIO header for hardware PWM:
Control LED using Python Let’s turn an ON and OFF LED using Python on Raspberry Pi. The switch is used to control the LED ON- OFF. Python Program for Raspberry Pi to control LED using Push Button import RPi.GPIO as GPIO #import RPi.GPIO module LED = 32 #pin no. as per BOARD, GPIO18 as per BCM Switch_input = 29 #pin no. as per BOARD, GPIO27 as per BCM GPIO.setwarnings(False) #disable warnings GPIO.setmode(GPIO.BOARD) #set pin numbering format GPIO.setup(LED, GPIO.OUT) #set GPIO as output GPIO.setup(Switch_input, GPIO.IN, pull_up_down=GPIO.PUD_UP) while True: if(GPIO.input(Switch_input)): GPIO.output(LED,GPIO.LOW) else: GPIO.output(LED,GPIO.HIGH) Functions Used: RPi.GPIO To use Raspberry Pi GPIO pins in Python, we need to import RPi.GPIO package which has class to control GPIO. This RPi.GPIO Python package is already installed on Raspbian OS. So, we don’t need to install it externally. Just, we should include library in our program to use functions for GPIO access using Python. This is given as follows. import RPi.GPIO as GPIO GPIO modes:
- The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin the the plug
- i.e the numbers printed on the board (e.g. P1).
- The GPIO.BCM option means that you are referring to the pins by the "Broadcom SOC channel" number, these are the numbers after "GPIO"
- Unfortunately the BCM numbers changed between versions of the Pi1 Model B. – The Model B+ uses the same numbering as the Model B r2.0, and adds new pins (board numbers 27-40). – The Raspberry Pi Zero, Pi 2B and Pi 3B use the same numbering as the B+. GPIO.setmode ( Pin Numbering System ) This function is used to define Pin numbering system i.e. GPIO numbering or Physical numbering. In RiPi.GPIO GPIO numbering is identified by BCM whereas Physical numbering is identified by BOARD Pin Numbering System = BOARD/BCM E.g. If we use pin number 40 of P1 header as a GPIO pin which we have to configure as output then, In BCM , GPIO.setmode(GPIO.BCM) GPIO.setup(21, GPIO.OUT) In BOARD, GPIO.setmode(GPIO.BOARD) GPIO.setup(40, GPIO.OUT) GPIO.setup (channel, direction, initial value, pull up/pull down) This function is used to set the direction of GPIO pin as an input/output. channel – GPIO pin number as per numbering system.
direction – set direction of GPIO pin as either Input or Output. initial value – can provide initial value pull up/pull down – enable pull up or pull down if required Few examples are given as follows:
- GPIO as Output GPIO.setup (channel, GPIO.OUT)
- GPIO as Input GPIO.setup (channel, GPIO.IN)
- GPIO as Output with initial value GPIO.setup (channel, GPIO.OUT, initial=GPIO.HIGH)
- GPIO as Input with Pull up resistor GPIO.setup (channel, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.output(channel, state) This function is used to set the output state of GPIO pin. channel – GPIO pin number as per numbering system. state – Output state i.e. HIGH or LOW of GPIO pin e.g. GPIO.output(7, GPIO.HIGH) GPIO.input(channel) This function is used to read the value of GPIO pin. e.g. GPIO.input(9) Control LED using C (WiringPi) We can access Raspberry Pi GPIO using C. Here, we are using WiringPi library for accessing Raspberry Pi GPIO using C. C (WiringPi) Program for Raspberry Pi to control LED using Push Button #include #include int LED = 26; /* GPIO26 as per wiringPi, GPIO12 as per BCM, pin no.32 / int switch_input = 21; / GPIO21 as per WiringPi, GPIO5 as per BCM, pin no.29 / int main(){ wiringPiSetup(); / initialize wiringPi setup / pinMode(LED,OUTPUT); / set GPIO as output / pullUpDnControl(switch_input, PUD_UP); while (1){ if(digitalRead(switch_input)) digitalWrite(LED,LOW); / write LOW on GPIO / else digitalWrite(LED, HIGH); / write HIGH on GPIO */ } } Machine Learning Model A machine learning model is a mathematical program that identifies patterns in training data to make predictions or decisions on unseen data without being explicitly programmed to do so. It is built by feeding an algorithm—like a decision tree or neural network—historical data to optimize its parameters.
- Semiconductor IC Sensors: Integrated silicon chips providing direct analog or digital outputs. Popular low-cost options like the DHT11 or DS18B20 operate cleanly between - 55 °C and 150 °C for smart home automation. LM35 Temperature Sensor LM35 is a precession Integrated circuit Temperature sensor, whose output voltage varies, based on the temperature around it. It is a small and cheap IC which can be used to measure temperature anywhere between - 55°C to 150°C. It can easily be interfaced with any Microcontroller that has ADC function or any development platform like Arduino. Power the IC by applying a regulated voltage like +5V (VS) to the input pin and connected the ground pin to the ground of the circuit. Now, you can measure the temperature in form of voltage. If the temperature is 0°C, then the output voltage will also be 0V. There will be rise of 0.01V (10mV) for every degree Celsius rise in temperature. The voltage can converted into temperature using the below formulae: Features of LM35 Sensor ● Minimum and Maximum Input Voltage is 35V and - 2V respectively. Typically 5V. ● Can measure temperature ranging from - 55°C to 150°C ● Output voltage is directly proportional (Linear) to temperature (i.e.) there will be a rise of 10mV (0.01V) for every 1°C rise in temperature. ● ±0.5°C Accuracy ● Drain current is less than 60uA ● Low cost temperature sensor ● Small and hence suitable for remote applications ● Available in TO-92, TO-220, TO-CAN and SOIC package
DHT11 Sensor DHT11 sensor is used to measure the temperature and humidity. It has a resistive humidity sensing component and a negative temperature coefficient (NTC). An 8 bit MCU is also connected in it which is responsible for its fast response. It is very inexpensive but it gives values of both temperature and humidity at a time. It’s a very basic and slow sensor easy to use for small scale. The DHT sensors are made of two parts, a capacitive humidity sensor and thermistor. There is also a very basic chip inside that does some analog to digital conversion and spits out a digital signal with the temperature and humidity. The digital signal is fairly easy to read using any microcontroller. These sensors are frequently used in remote weather stations, soil monitors, and home environment control systems. There are two variants of the DHT11. One is a four pin standalone module, and the other is a three pin, PCB mounted module. The pin out is different for each one, so connect the DHT11 according to which one you have. Features of DHT11 Sensor
- Ultra-low cost
- 3 to 5V power and I/O
- 2.5mA max current use during conversion (while requesting data)
- Good for 20–80% humidity readings with 5% accuracy
- Good for 0–50°C temperature readings ±2°C accuracy
- No more than 1 Hz sampling rate (once every second)
- Body size 15.5mm x 12mm x 5.5mm
- 4 pins with 0.1" spacing
Windows
- Visit the official Thonny Website.
- Hover over the Windows download box in the upper right corner.
- Click the first link to download the .exe installer file (includes Python bundle).
- Double-click the downloaded installer.
- Follow the setup wizard instructions (leave settings as default).
- Launch Thonny from your Start Menu or Desktop shortcut. Linux (Ubuntu / Debian / Mint) You can install Thonny using the built-in package manager or the official automated script to guarantee the absolute newest version. Option A: Official Bash Script (Recommended for Latest Version) Open a terminal and run the official script provided by the Thonny Linux Guide: bash wget - qO- https://thonny.org/installer-for-linux | bash Option B: Standard Package Manager (Quickest) Open your terminal and execute these commands to download dependencies and install the app: bash sudo apt update sudo apt install python3-tk thonny - y Connecting to a Raspberry Pi Pico / Pico 2 Once Thonny is open on your machine, configuring it for physical microcontrollers takes just a few clicks:
- Connect your Pico to your computer using a USB cable.
- Navigate to the top menu bar inside Thonny and click Run ➔ Configure interpreter...
- Under the Interpreter tab, click the dropdown menu and select MicroPython (Raspberry Pi Pico).
- Set the Port dropdown to automatic detection or select the corresponding COM/TTY port.
- Click OK , and the bottom Shell area will display the MicroPython prompt. Implementation Apparatus: Raspberry Pi board, USB Cable, DHT 22 Sensor. Interface/Connections: Procedure: Step 1: Connect the Raspberry Pi board to CPU using the FRC cable provided with the board. Step 2: Connect the Power supply adaptor and power on the circuit. Step 3: Open Thonny IDE from the Raspberry Pi desktop application menu (under the Programming tab). Step 4: In Thonny IDE, go to Tools → Manage Packages, search for adafruit-circuitpython-dht, and click install to set up the library for reading sensor data. Step 5: In Thonny IDE, go back to Tools → Manage Packages, search for scikit-learn, and click install to load the Machine Learning ecosystem. Step 6: Create a new Python file in Thonny IDE, write the required code script combining the sensor integration and a linear regression or time-series prediction model, and click Save. Step 7: Click the green Run current script button in the Thonny IDE toolbar to compile, establish the pipeline, and execute the Python script. Step 8: Observe the live real-time metrics and future predicted temperature variables updating continuously within the built-in Thonny IDE terminal shell. Step 9: Put the DHT22 Sensor in different physical environments (e.g., near a heat source or cold area) and observe how the ML model adapts its future predictions. Peripheral Raspberry Pi Pin DHT 22 VCC 5V DHT 22 GND GND DHT 22 SDA GPIO2 8 DHT22 NC -
Observations: Thonny IDE Shell connects to the DHT22 sensor via the designated GPIO pin. When the DHT22 Sensor is placed in different environments, it prints its surrounding real-time temperature values directly to the built-in terminal. This data is instantly processed locally by the ML model, which computes trends and prints the predicted future temperature values side-by-side with the live readings. Observation Table: Conclusion: An application was developed which collects real-time temperature data using an IoT sensor and apply a machine learning model to predict future temperature values using a Machine Learning model successfully. Viva Questions:
- What are the GPIO pins in R aspberry Pi?
- Expl ain functions of GPIO pins of Raspberry Pi.
- What do you mean by numbering in Raspberry Pi?
- How many numbering are there in Raspberry Pi? Explain.
- What are the different types o f modes of GPIO in Raspberry Pi?
- What is a Machine Learning Model?
- What are the types of Machine Learning?
- What are the different types of Machine Learning Model used in IoT?
- What is a Temperature Sensor? 1 0. What are the types of Temperature Sensor?
- Wha t is a LM35 Sensor?
- What is a DHT11 Sensor?
- What is the difference between DHT11 Sensor and DHT22 Sensor?
- Can Arduino IDE run Raspberry Pi?
- Can Arduino IDE run Machine Learning?
- Can Arduino IDE run Python?
- What is Thonny IDE?
- What are the different models of Raspberry P i? What is the latest version?
- Do all Rasp berry Pi models use same number of GPIO Pins?
- Can Raspberry Pi connect with Ard uino Uno?
- Explain dht library of Python.
- Explain time library of Python.
- Exp lain DHT 22 () function.
- Explain measure() function.
- Explain temperature() fu nction.
- What are the components & pins of DHT 11 Sensor?
- What are the components & pins of LM35 Sensor?
- What are the features of DHT 11 Sensor?
- What are the features of LM35 Sens or? DHT22 Surrounding Environment State Temperature Predicted Temperature Room Warm 25 27 Outside Room Rainy 11 17 Nature Cloudy 22 23.