python and arduino


I am working on a underwater robotics competition (MATE, ROV) and will be implementing arduino as controls. We will be having 2 sensors, a pressure and temp. I have a simple GUI thing written up in python, here it is:

app = QtGui.QApplication(sys.argv)
window = Window()

if name == 'main':import sys
from PyQt4 import QtGui

class Window(QtGui.QMainWindow):
def init(self):
super(Window, self).init()

set central widget

widget = QtGui.QWidget(self)

set layout

layout = QtGui.QGridLayout(self)

create temperature stuff

layout.addWidget(QtGui.QLabel('Temperature', widget), 0, 0)
self.lbl_temp = QtGui.QLabel('34F', widget)
layout.addWidget(self.lbl_temp, 0, 1)

create pressure stuff

layout.addWidget(QtGui.QLabel ('Pressure', widget), 1, 0)
self.lbl_pressure = QtGui.QLabel('20psi', widget)
layout.addWidget(self.lbl_pressure, 1, 1)

def main():


I just have random numbers for the sensors as of now. Sooooo, my question is how can i get the sensor data from the arduino and to the laptop running the python code? Does anyone have any example codes? Oh, also the arduino will be connected to the computer with ethernet in the end but for now it is just the normal USB connection. Right now i just need for the arduino to communicate with the computer but will aventially need the sensors, so if you have a code for either one please help me out!


This Python - Arduino demo should point you in the right direction. Also look at the more recently written Serial Input Basics

You may also be interested in this Python GUI demo


any other tips or codes?

any other tips or codes?

You need to tell me what you need that I have not already covered.


Sorry! that was just general if anyone else has any input, sorry again!


So what is wrong with my stuff?

Feedback is always useful. Maybe I can improve something.


Nothing is. oh on the link you gave me, I can open the arduino file but cant open the python file? would different versions of python do this? Would different bit rates do this (I'm running 32 bit)?


but cant open the python file?

I presume you mean the file

What do you mean by “can’t open” - please describe what you have tried. I presume you can view the file in your text editor?

Are you getting error messages when you try to run the program. If so please post them.

What operating system are you using?

The Python code was written with Python 2.7. I have not tried it with Python 3.


To be honest I'm not sure what exactly is wrong, im running windows 10 on 32 bit. could you paste the python code hear please?

To be honest I’m not sure what exactly is wrong,

Unless you provide a lot more specific information I cannot provide any useful advice. You have not answered any of my questions in Reply #7.

This is the code for the file - if I could download it to include it here I’m sure you could also do so.

# 19 July 2014

# in case any of this upsets Python purists it has been converted from an equivalent JRuby program

# this is designed to work with ... ArduinoPC2.ino ...

# the purpose of this program and the associated Arduino program is to demonstrate a system for sending 
#   and receiving data between a PC and an Arduino.

# The key functions are:
#    sendToArduino(str) which sends the given string to the Arduino. The string may 
#                       contain characters with any of the values 0 to 255
#    recvFromArduino()  which returns an array. 
#                         The first element contains the number of bytes that the Arduino said it included in
#                             message. This can be used to check that the full message was received.
#                         The second element contains the message as a string

# the overall process followed by the demo program is as follows
#   open the serial connection to the Arduino - which causes the Arduino to reset
#   wait for a message from the Arduino to give it time to reset
#   loop through a series of test messages
#      send a message and display it on the PC screen
#      wait for a reply and display it on the PC

# to facilitate debugging the Arduino code this program interprets any message from the Arduino
#    with the message length set to 0 as a debug message which is displayed on the PC screen

# the message to be sent to the Arduino starts with < and ends with >
#    the message content comprises a string, an integer and a float
#    the numbers are sent as their ascii equivalents
#    for example <LED1,200,0.2>
#    this means set the flash interval for LED1 to 200 millisecs
#      and move the servo to 20% of its range

# receiving a message from the Arduino involves
#    waiting until the startMarker is detected
#    saving all subsequent bytes until the end marker is detected

#       this program does not include any timeouts to deal with delays in communication
#       for simplicity the program does NOT search for the comm port - the user must modify the
#         code to include the correct reference.
#         search for the lines 
#               serPort = "/dev/ttyS80"
#               baudRate = 9600
#               ser = serial.Serial(serPort, baudRate)


#  Function Definitions


def sendToArduino(sendStr):


def recvFromArduino():
  global startMarker, endMarker
  ck = ""
  x = "z" # any value that is not an end- or startMarker
  byteCount = -1 # to allow for the fact that the last increment will be one too many
  # wait for the start character
  while  ord(x) != startMarker: 
    x =
  # save data until the end marker is found
  while ord(x) != endMarker:
    if ord(x) != startMarker:
      ck = ck + x 
      byteCount += 1
    x =


def waitForArduino():

   # wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
   # it also ensures that any bytes left over from a previous message are discarded
    global startMarker, endMarker
    msg = ""
    while msg.find("Arduino is ready") == -1:

      while ser.inWaiting() == 0:
      msg = recvFromArduino()

      print msg

def runTest(td):
  numLoops = len(td)
  waitingForReply = False

  n = 0
  while n < numLoops:

    teststr = td[n]

    if waitingForReply == False:
      print "Sent from PC -- LOOP NUM " + str(n) + " TEST STR " + teststr
      waitingForReply = True

    if waitingForReply == True:

      while ser.inWaiting() == 0:
      dataRecvd = recvFromArduino()
      print "Reply Received  " + dataRecvd
      n += 1
      waitingForReply = False

      print "==========="





import serial
import time


# NOTE the user must ensure that the serial port and baudrate are correct
serPort = "/dev/ttyS80"
baudRate = 9600
ser = serial.Serial(serPort, baudRate)
print "Serial port " + serPort + " opened  Baudrate " + str(baudRate)

startMarker = 60
endMarker = 62


testData = []



As you are using Windows you will have to edit the file to put in the correct serial port reference.


but cant open the python file?

Could it be that the python file can't open the COM port?

Instead of:
    serPort = "/dev/ttyS80"
you have to use:
    serPort = "COM1"

Replace COM1 by the used COM port.


Sorry been real busy lately, but got everything running! thanks

Sorry been real busy lately, but got everything running! thanks

It would be useful for others if you explain what you did.