0% found this document useful (0 votes)
2 views

Lab_2

The document outlines the process of acquiring sensor data using an Arduino and Python. It includes setup instructions, example Arduino code for reading a potentiometer, and Python scripts for reading, displaying, and visualizing the data. Additionally, it provides an assignment to extend the project by acquiring data from five sensors and storing it in a CSV file.

Uploaded by

ahmedna694
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab_2

The document outlines the process of acquiring sensor data using an Arduino and Python. It includes setup instructions, example Arduino code for reading a potentiometer, and Python scripts for reading, displaying, and visualizing the data. Additionally, it provides an assignment to extend the project by acquiring data from five sensors and storing it in a CSV file.

Uploaded by

ahmedna694
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Acquiring Sensor Data

Environment
• A computer running Python

• An Arduino

• A potentiometer (the sensor)

• Wires, a resistor, an LED, and a breadboard to connect the sensor to the


Arduino

• A USB cable to connect the Arduino to the computer

1
Acquiring Sensor Data

2
An Arduino Program
//reads a potentiometer and sends value over serialint sensor

Pin = A0; // The potentiometer on pin 0


int ledPin = 13; // The LED is connected on pin 13
int sensorValue; // variable to stores data

void setup() // runs once when the sketch starts


{
pinMode(ledPin, OUTPUT); // make the LED pin (pin 13) an output pin
Serial.begin(9600); // initialize serial communication
}

void loop() // runs repeatedly after setup() finishes


{
sensorValue = analogRead(sensorPin); // read pin A0
Serial.println(sensorValue); // send data to serial

if (sensorValue < 500) // less than 500?


digitalWrite(ledPin, LOW); // Turn the LED off
else // greater than 500?
digitalWrite(ledPin, HIGH); // Keep the LED on

delay(100); // Pause 100 milliseconds


}
3
Acquiring Sensor Data
# serial read using the Python

>>> import serial


>>> import time
>>> ser = serial.Serial('COM4',9600)
>>> time.sleep(2)
>>> b = ser.readline()
>>> b
b'409\r\n’
>>> type(b)
<class 'bytes’

4
Acquiring Sensor Data
# serial read using the Python

>>>> str_rn = b.decode()


>>> str_rn
'409\r\n’
>>> str = str_rn.rstrip()
>>> str'409’
>>> type(str)
<class 'str’>
>>> f = float(str)
>>> f409.0
>>> type(f)
<class 'float'>

5
Acquiring Sensor Data
# a Python script to read the sensor

import serial

import time

6
Acquiring Sensor Data
# a Python script to read the sensor

# set up the serial line

ser = serial.Serial('COM4', 9600)

time.sleep(2)

7
Acquiring Sensor Data
# Read and record the data

data =[] # empty list to store the data

for i in range(50):

b = ser.readline() # read a byte string

string_n = b.decode() # decode byte string into Unicode

string = string_n.rstrip() # remove \n and \r

flt = float(string) # convert string to float

data.append(flt) # add to the end of data list

time.sleep(0.1) # wait (sleep) 0.1 seconds

ser.close()

8
Displaying Sensor Data
# a Python script to display the collected data

# show the data

for i in data:

print(i)

9
Displaying Sensor Data
# a Python script to display the collected data

# show the data

for i in data:

print(i)

10
Visualizing Sensor Data
# a Python script to plot the collected data

import matplotlib.pyplot as plt

plt.plot(data)

plt.xlabel('Time (seconds)’)

plt.ylabel('Potentiometer Reading’)

plt.title('Potentiometer Reading vs. Time’)

plt.show()

11
Visualizing Sensor Data

12
Assignment
# Write a Python script to

• Acquire data of five sensors from the serial port


number '9600' of Arduino, there is a delay equaled 0.1
second between the sent data of each sensor.

• Convert the collected data into float format.

• Individually display the collected data of each


sensor.

• Individually visualize the collected data of each


sensor.

• Store the collected data of each sensor into CSV file.

13

You might also like