Anda di halaman 1dari 36

Arduino 02:

Using the Arduino with


Python
Jeffrey A. Meunier
jeffm@engr.uconn.edu
University of Connecticut
About:
How to use this document
I designed this tutorial to be tall and
narrow so that you can read it on one
side of your screen while you follow
along in the rest of the display area, like
this:

Tutorial

work
area

Also, the pages in this tutorial are


designed to be viewed as whole-page
slides. I describe how to do that next.

2
Set your PDF viewer to single
page mode (Mac)
On a Mac, a PDF file will open in the
Preview application by default. Hide the
side bar and set it to Single Page.

3
Set your PDF viewer to single
page mode (Windows)
In Windows, a PDF file will open in the
Reader application by default. You can
open the menu from the icon in the
upper left corner of the window and then
choose Split Left or Split Right depending
on your preference.

Then right-click on the document and


choose the One page viewing option.

4
Introduction

In this document I show you how to get


Python to communicate with your
Arduino microcontroller board. This
requires that you download a few files
from my web site and install one
standard module into Python.

Ultimately, after all the initial setup is


done, writing a Python program that uses
the Arduino is not difficult at all.

If you are an experienced Python


programmer and Arduino user, you might
want to jump ahead to see if the
Summary and Appendix are all you need.

5
Objectives

The objectives are:


• Installing a Python module with pip
• Downloading a Python module
• Writing a program in IDLE
• Communicating with the Arduino

6
Prerequisites

Before proceeding with this tutorial


Python must be installed on your
computer. This tutorial shows examples
using IDLE.

You must also have installed firmware on


the Arduino from a previous one of my
tutorials.

In this tutorial you will not need to use


the Arduino application.

7
Outline

These are the sections you'll go through


in this exercise:

1. Install pyserial
2. Download the arduino.py module
3. Write a new program in IDLE
4. Enter some Arduino commands
5. Summary

8
1. Install pyserial
Introduction
Python uses the USB connection to
communicate with the Arduino. It needs
to use the pyserial USB communications
module, but this module isn't installed in
Python by default. Here you will install
that module, but how you install it
depends on what computer you're using.

1. Your own Windows computer

2. Your own Mac or Linux computer

3. A computer in one of the UConn


Learning Center labs

9
1. Install pyserial
About pip
If you are using your own computer then
you will use the pip command to install
the pyserial module in Python.

The pip command is a recursive acronym


(i.e., the acronym is part of the full
name):

PIP Installs Packages

Recursive acronyms are common in


computer science.

Now skip to the page for your computer.


It will have one of these logos in the
corner:

10
1. Install pyserial with pip:
Mac & Linux
Open the Terminal application and enter
the command:
pip3 install pyserial

Do not start Python first. You should see


this in the Terminal window:

The exact version number shown for the


pyserial module might be different than
what I show you, but it should show
pyserial-x.x on the last line.

Remember that your pip command is


actually pip3.

11
1. Install pyserial with pip:
Windows
Open the cmd (Command Prompt)
application and enter the command:
pip install pyserial

Do not start Python first. You should see


this in the Command Prompt window:

The exact version number shown for the


pyserial module might be different than
what I show you, but it should show
pyserial-x.x on the last line.

12
1. Install pyserial:
UConn Learning Center
Download the pyserial.zip file from this
web page:
http://www.engr.uconn.edu/~jeffm/Arduino/Fir
mware/index.html

Save the file to your desktop. It should


look like this:

13
1. Install pyserial:
UConn Learning Center
Double-click on the pyserial ZIP file

to view its contents:

Drag that serial folder onto the desktop.

You may now delete the pyserial file from


the desktop.

14
1. Install pyserial:
UConn Learning Center
Create a new folder for this project on your P:
drive. Name the folder based on what course &
project this is.
• CSE1010 / Lab n
• CSE1010 / Homework n
• Engr1166 / Project n

Move the serial folder from the desktop into the


folder you just created.

15
Outline

These are the sections you'll go through


in this exercise:

√ 1. Install pyserial
2. Download the arduino.py module
3. Write a new program in IDLE
4. Enter some Arduino commands
5. Summary

16
2. Download arduino.py

Go to this web site:


http://www.engr.uconn.edu/~jeffm/Arduino/Su
pport/index.html

Right-click and save the arduino.py file:

Save it in a new folder. Name the folder


based on what course & project this is.
• CSE1010 / Lab n
• CSE1010 / Homework n
• Engr1166 / Project n

Learning center students: put it in the


same folder on your P: drive. 17
Outline

These are the sections you'll go through


in this exercise:

√ 1. Install pyserial with pip


√ 2. Download the arduino.py module
3. Write a new program in IDLE
4. Enter some Arduino commands
5. Summary

18
3. Write a program in IDLE
Motivation
Here I have you write a program that
doesn't have any actual Python
statements in it, just a single comment
line.

The reason I have you do this is so that


when you load the program into the
Python Shell, it will make the folder you
save it in the active folder.

19
3. Write a program in IDLE
Open a new file
Choose File → New File from the Python
Shell window. This will open a new text
editor window in which you can write a
Python program.

Enter this single Python comment line in


the text editor:

# Arduino test program 1

20
3. Write a program in IDLE
Save the file
Press F5 in order to run the program in
the Python Shell window. It will give you
the message Source Must Be Saved, OK
to Save? Press the OK button.

Navigate to the same folder where you


saved the arduino.py file. Name this file
test1.py, then save it. The Python Shell
window moves to the foreground and
becomes the active window. It should
have a RESTART message in it:

21
Outline

These are the sections you'll go through


in this exercise:

√ 1. Install pyserial with pip


√ 2. Download the arduino.py module
√ 3. Write a new program in IDLE
4. Enter some Arduino commands
5. Summary

22
4. Enter Arduino commands
First statement
Here you will have Python send a few
commands to the Arduino to switch the
Arduino's on-board LED on and off. The
commands will be entered into the
Python Shell window.

Enter the following statements into the


Python Shell window. After each
statement (in bold) I explain what the
statement does.

import arduino

That command tells Python to import the


arduino.py module file that you
downloaded and saved already.
Everything in that module immediately
becomes available for your use in Python.

23
4. Enter Arduino commands
Create Arduino instance
a = arduino.Arduino()

That statement creates a new Arduino


object in Python's memory and stores it
in a variable called a. Explaining what it
does and how it does it is beyond the
scope of this tutorial.

[Note that the thing in the variable a is


an instance of the Arduino class that I
wrote in the arduino.py module. If you're
interested, have a look in that module to
see how I implemented it.]

24
4. Enter Arduino commands
Serial connection
a.serialConnect()

That statement causes Python to open a


connection to the Arduino board over the
USB cable, and stores the connection
information inside the Arduino object
stored in the variable a.

If it worked correctly then you'll just see


the Python prompt again.

>>>

However, you might see an error. (next


slide)

25
4. Enter Arduino commands
Serial connection error
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
a.serialConnect()
...
Exception: No USB devices found

If you see that error, then do the


following:
• Ensure that your Arduino board is
plugged into your computer.
• Ensure that the Arduino application is
not running (i.e., exit out of it
completely).

Now try again!

26
4. Enter Arduino commands
Still see the error?
If you still get the error, then you should
re-enter the a.serialConnect() statement
but this time supply the name of the
serial port as an argument. Remember in
a previous tutorial when I asked you to
write down the name of your serial port?
This is where you're going to need it.

Retype the command like this:

a.serialConnect(your-port-name)

Replace your-port-name with the name


of your USB port, like for example
'/dev/cu.usbmodem1411' (on a Mac) or
'COM7' (in Windows).

Be sure to enclose it in quotes in order to


make it a string.
27
4. Enter Arduino commands
It should now be working
I'll assume now that you got it working.

For whatever project you do that uses


the Arduino, you will need to do those
first three statements only one time at
the beginning of your program.

import arduino

a = arduino.Arduino()
a.serialConnect()

28
4. Enter Arduino commands
It should now be working
I'll assume now that you got it working.

For whatever project you do that uses


the Arduino, you will need to do those
first three statements only one time at
the beginning of your program.
Many students
import arduino forget these
parentheses

a = arduino.Arduino()
a.serialConnect()

29
4. Enter Arduino commands
Set the pin mode
Enter this next statement in Python:

a.pinMode(13, 'o')

That tells the Arduino that you're going


to do output on pin number 13 of the
Arduino.

Each digital pin can do either input or


output, but not at the same time. Use 'o'
for output or 'i' for input.

30
4. Enter Arduino commands
Turn the LED on
a.digitalWrite(13, 1)

Have a look at the Arduino board. A small


red LED in the middle of the board should
be glowing. (It might be a different color
or in a different location if you're not
using the RoboRED board.)

Now enter this command:

a.digitalWrite(13, 0)

That turns the LED off.

31
4. Enter Arduino commands
More commands
At this point you may do as many
digitalWrite, digitalRead, analogWrite,
or analogRead statements as you like
within your program. The statements are
described in Appendix A of this
document.

32
4. Enter Arduino commands
Disconnect from USB
a.serialDisconnect()

That tells Python that you're finished


using the Arduino board for now. It closes
the USB connection between Python and
the Arduino.

In your own programs you need to do


that statement only one time just before
the program ends.

33
Outline

These are the sections you'll go through


in this exercise:

√ 1. Install pyserial with pip


√ 2. Download the arduino.py module
√ 3. Write a new program in IDLE
√ 4. Enter some Arduino commands
5. Summary

34
5. Summary

Ensure that my ArduinoFirmware file was


the last file loaded onto the Arduino. You
don't need to re-upoad it.

Create a new folder. Copy the arduino.py


module into that folder.

Start IDLE, create a New File.

import arduino


a = arduino.Arduino()

a.serialConnect()

# -> your program goes here



# at end of program:

 .serialDisconnect()
a

35
Outline

These are the sections you'll go through


in this exercise:

√ 1. Install pyserial with pip


√ 2. Download the arduino.py module
√ 3. Write a new program in IDLE
√ 4. Enter some Arduino commands
√ 5. Summary

You're done!

36

Anda mungkin juga menyukai