Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

Interfacing the Arduino Serial Monitor with Python

Contents

Introduction

In this tutorial, we will use a python script as our Serial Monitor. You may ask, why do we need to use python for this when there is a built-in Serial Monitor in our Arduino IDE?

Python is currently the most popular programming language out there. Python is used by people belonging to every field imaginable for its ease of access, simplicity and any difficult tasks can be achieved easily by using python. Currently, it is the most popular choice for Machine Learning, Deep Learning and Data Science.

Therefore, if we are able to interface the sensor data with python, we can achieve greater things with it, for example, we can use the sensor data to train machine learning algorithms or for data visualization as well as saving data or create CSV files containing sensor data.

Schematics

We are using an LDR and analog pin A0 to determine the level of light in our environment.

The Code

				
					void setup(){   
     pinMode(A0, INPUT);   
     Serial.begin(9600); 
} 
void loop(){   
     Serial.println(analogRead(A0));   
     delay(100); 
}
				
			

Interfacing The Serial Monitor Using Python

So, far we have implemented a voltage divider with a resistor and LDR, Using the A0 analog pin to take input and print the values at the Serial.

  • First of all, make sure you have Python 3 installed, which can be downloaded from: www.python.org/downloads/
  • To interface the Serial monitor using Python, we will be using a module called “PySerial”. For more details on Pyserial, Check its GitHub page: github.com/pyserial/pyserial
  • To install the PySerial module, Open the command prompt, and type “pip install pyserial” and press Enter.
  • PySerial should be installed on your computer. To check if it is working properly, open your command prompt again and type “python” > “import serial”

Now, let us start writing our script. Write the following code and save it.

When naming your file DO NOT name it “serial.py”, as it can cause issues with your script and will eventually stop working.

				
					import serial
com = “COM3” 
baud = 9600 
ser = serial.Serial(com, baud, timeout = 0.1) 
while ser.isOpen(): 
     print(ser.readline())
				
			

Name the com variable according to the com port you Arduino uses. In my case, it is COM3. Might be different for you. Open up your Arduino IDE > Tools > Port

Name the ‘com’ variable exactly what it shows there. Otherwise, the script will not work.

NOTE: make sure the serial monitor on the Arduino IDE is closed.

Now, Open the python script and you should see your sensor data starting to be displayed!

Right now the sensor data is being shown, we can clean the string up to only show the numerical values, filtering out the unnecessary characters and alike which we will run through in a follow-up tutorial.

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top