Book Image

Raspberry Pi cookbook for Python programmers

Book Image

Raspberry Pi cookbook for Python programmers

Overview of this book

Table of Contents (18 chapters)
Raspberry Pi Cookbook for Python Programmers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Controlling USB devices


The Universal Serial Bus (USB) is used extensively by computers to provide additional peripherals and expansion through a common standard connection. We will use the Python library PyUSB to send custom commands to connected devices over USB.

The following example controls a USB toy missile launcher, which in turn allows it to be controlled by our Python control panel. We see that the same principle can be applied to other USB devices, such as a robotic arm, using similar techniques, and the controls can be activated using a sensor connected to the Raspberry Pi GPIO.

The USB Tenx Technology SAM missile launcher

Getting ready

We will need to install PyUSB for Python 3 using pip-3.2 as follows:

sudo pip-3.2 install pyusb

You can test whether PyUSB has installed correctly by running the following:

python3
> import usb
> help (usb)
> exit()

This should allow you to view the package information if it was installed correctly.

How to do it...

We will create the following...