Notifications
Clear all

Arduino and Python

7 Posts
3 Users
1 Likes
2,948 Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

I got my first Arduino a couple of years ago and as most people do one of the first projects I endeavored to achieve was to hook up the Arduino to a DHT sensor. Not satisfied with that I added 433Mhz transmitters/receivers for measuring the environment in the bedroom/living room/kitchen/attic/cellar etc.

Sending all the info back to one receiver. I was pleased as Punch!

Then I decided that I wanted to log the data and have a nice graphics display on my computer. For this, I used Python and TKinter.

So I wrote the program in Python to get the data from the USB and display on the screen.

I want to share some code which was the kicking off point to the development, rather than publish the whole Python program and the Arduino sketch which not easily readable for the beginner. This is just for motivation purposes.

First the Arduino sketch

void setup() {
Serial.begin(9600);

}

void loop() {
// Change this string to anything you like
// but remember to leave \n at the end, as this
// marker is required by Python readline()

Serial.write("I am the Walrus, Coo Coo Ca Choo\n");
delay(5000);

}

Now the Python code

#!/usr/bin/python

import serial

import time

# you may need to run pip install pyserial first

# change port to port name you are using

# to get the list of available ports

# run "python -m serial.tools.list_ports" command in the terminal

# initialise the serial object

ser = serial.Serial(

port='/dev/cu.usbmodem1461',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=None

)

print("connected to: " + ser.portstr)

while True: # loop forever
print ("Waiting....")
# get data from serial port
byteArray = ser.readline() # reads the incoming bytearray until a linefeed (\n) is encountered
print(byteArray) # prints the received data array it's raw form

mList = [] # initialise zero list

for mByte in byteArray:
if mByte !=10: # ignore the linefeed
# print the byte value, then a space, then the equivalent ASCII code
print(str(mByte) +" "+chr(mByte))
# append bytes to list
mList.append(mByte)

# print full byte list
print(mList)

# just for fun, print the timestamp of the incoming data
localtime = time.asctime( time.localtime(time.time()) )
print(localtime)

# clear serial buffer
ser.reset_input_buffer()

ser.close()

I have tried to make this very self-explanatory and as simple as possible as a jumping off point for anyone who cares to use it.

I also use Python/Arduino for setting the date/time on RTC chips and getting CSV data from my hard drive.

Have fun expanding on this! It is all about the learning process!


   
jscottbee reacted
Quote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 

@pugwash

Great example Steve.  I love the way you kept the example as simple as possible.  Great work!

I do have a question concerning hardware and OS.    What are  you running the python on? 

I notice that when you open the serial  port you use the following code:

port='/dev/cu.usbmodem1461',\

Is that port going to be machine or OS dependent? For example, if I ran this Python code on a Raspberry Pi would it still work?

I'm looking forward to trying to run this on my Windows 10 Notebook computer.   But I don't have time to do it at the moment.   But my first thought was to wonder if it would also run on a Raspberry Pi, or if it would need to be modified to run on the Pi.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

Python, in my case, is running on MacOS but that is irrelevant, just use the same port name as shown in the Arduino IDE. Or check the System Info on Windows. It should run on a RaspPI as well.

Can't say much more about Windows operating system, because my last Windows computer ran 98SE and landed in the corner of my office in pieces after the 10th BSOD in one day, and being thrown with considerable force at the wall.


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 

Thanks for the info.  I'll see if I can find time to try on my Windows notebook and see if it works.  Then maybe later I'll try it on a Pi.   I'm only just now learning to use Python.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

The most significant discovery I made when I first ran this code was that the Arduino communicates exclusively with the outside world using byte arrays, both for serial and I2C communication.

At that point, the penny dropped and explained why some of the things I was trying to do, just didn't work.

I was thinking too much in terms of a microprocessor and not enough about microcontrollers.


   
ReplyQuote
jscottbee
(@jscottbee)
Member
Joined: 5 years ago
Posts: 107
 
This post was modified 5 years ago by jscottbee

   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 

Thanks for sharing your Tkinter GUI code Scott.   I've messed around with Tkinter GUI before myself.  But  it's nice to have example code like you've posted to work from.

This DroneBot Forum was a GREAT idea that Bill had.   It's going to be a gold mine of resources.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote