In this article, you will learn how to use a Temperature Monitoring System Using Arduino and Python Live Data Plotting. Creating this project will let you do serial communication from Arduino to Python. This is very useful if you are gathering data live or constantly monitoring temperatures.
Temperature Monitoring System Using Arduino and Python Live Data Plotting: Creating the Device
Here are the steps in interfacing the sound module in Arduino.
- Gathering the Components
The first thing to do is to collect the hardware components for the Arduino device.
Arduino Uno
DHT22 Digital Humidity and Temperature Module - Connecting the Components
Connect the components to the Arduino Uno. Please refer to the wiring diagram below.
- Coding the Arduino
Third step is about coding the Arduino device.
- Upload the Sketch
After coding the Arduino, upload the sketch.
- Coding with Python
Next is to create a project in Python and write codes that will read the serial monitor of the Arduino, save it in a CSV file, and plot it on a graph.
To start this project, you need the following:
Temperature Monitoring System Using Arduino and Python Live Data Plotting: Detailed Explanation
Qty | Component |
1 | Arduino UNO |
1 | DHT22 Humidity and Temperature Module |
Arduino UNO
We will be using an Arduino Uno microprocessor board. Arduino Uno is suitable for any projects and is the cheapest and widely used microprocessor board in the Arduino family. This is great for all kinds of IoT projects.
Digital Humidity and Temperature Module
This project uses a DHT22 sensor. You can also use the DHT11 sensor module which is cheaper. Take note that the latter is less accurate. DHT22 will read every 2 seconds for it to detect temperatures accurately. Be careful with the timing in the code.
Wiring Diagram
For the wiring, you just need to connect the Signal to Digital Pin 2, the Vcc to 5V and the GND to the Ground pin of the Arduino. If you are using a standalone temperature sensor, it has an extra GND pin that you can leave unconnected.
Temperature Monitoring System Using Arduino and Python Live Data Plotting
For the code, you just need to allow the sensor to read every second and display it on the serial monitor. No texts or labels – just the value. Upload this to the Arduino board.
#include "TinyDHT.h" //if it doesn't turn orange like the others below, enclose it using double quotation
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);
int tempMode = 0;
int t;
int h;
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
t = dht.readTemperature(tempMode);
Serial.println(t);
delay(2000);
}
Python Code
Now that our device is ready, you can now create live data plotting. First is to install pyserial and matplotlib. Pyserial is the package that lets you communicate with the Arduino while the matplotlib creates the data plotting.
To do this, open PyCharm 2021 and go to View>Tool Windows, Python Packages.
A window will appear below. search for pyserial and matplotlib. Click install and wait for it to finish. after that you are good to go.
Now create a new project in PyCharm and paste the code below.
import serial
import time
import csv
import matplotlib
matplotlib.use("tkAgg")
import matplotlib.pyplot as plt
import numpy as np
matplotlib.use("tkAgg")
if __name__ == '__main__':
ser = serial.Serial('COM5')
ser.flushInput()
plot_window = 20
y_var = np.array(np.zeros([plot_window]))
plt.ion()
fig, ax = plt.subplots()
line, = ax.plot(y_var)
while True:
try:
ser_bytes = ser.readline()
try:
decoded_bytes = float(ser_bytes[0:len(ser_bytes) - 2].decode("utf-8"))
print(decoded_bytes)
except:
continue
with open("test_data.csv", "a") as f:
writer = csv.writer(f, delimiter=",")
writer.writerow([time.time(), decoded_bytes])
y_var = np.append(y_var, decoded_bytes)
y_var = y_var[1:plot_window + 1]
line.set_ydata(y_var)
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
fig.canvas.flush_events()
except:
print("Keyboard Interrupt")
break
Output
Now, run the program. The sensor reads the temperature and the Python program plots it. The temperature is at the Y-axis and the time is on the X-axis.
Summary
And that’s it! You have successfully created an Temperature Monitoring System Using Arduino and Python Live Data Plotting! All you need to do is code the sensor and assemble. Then, create a project in Python and install Pyserial and Matplotlib packages, check the code above and run the program! This project is versatile and can be combined with other projects.
Download
Related Articles
Inquiries
Feel free to write your questions about the Temperature Monitoring System Using Arduino and Python Live Data Plotting at the comments below.