Tutorial
work
area
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.
4
Introduction
5
Objectives
6
Prerequisites
7
Outline
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.
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.
10
1. Install pyserial with pip:
Mac & Linux
Open the Terminal application and enter
the command:
pip3 install pyserial
11
1. Install pyserial with pip:
Windows
Open the cmd (Command Prompt)
application and enter the command:
pip install pyserial
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
13
1. Install pyserial:
UConn Learning Center
Double-click on the pyserial ZIP file
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
15
Outline
√ 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
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.
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.
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.
21
Outline
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.
import arduino
23
4. Enter Arduino commands
Create Arduino instance
a = arduino.Arduino()
24
4. Enter Arduino commands
Serial connection
a.serialConnect()
>>>
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
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.
a.serialConnect(your-port-name)
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.
a = arduino.Arduino()
a.serialConnect()
29
4. Enter Arduino commands
Set the pin mode
Enter this next statement in Python:
a.pinMode(13, 'o')
30
4. Enter Arduino commands
Turn the LED on
a.digitalWrite(13, 1)
a.digitalWrite(13, 0)
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()
33
Outline
34
5. Summary
import arduino
a = arduino.Arduino()
a.serialConnect()
# at end of program:
.serialDisconnect()
a
35
Outline
You're done!
36