O'Reilly logo

Internet of Things Programming with JavaScript by Ruben Oliva Ramos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Controlling the Arduino board from Python

First we need to install the serial library, as this helps to communicate with Arduino via the USB port communication. Type the following command to install the library:

sudo apt-get install python-serial

The following code controls Arduino from Raspberry Pi; you can now either copy the code inside a file called ControlArduinoFromRasp.py, or just get the complete code from the folder for this project.

The following snippet imports the serial library in Python:

import serial 

We define the serial communication:

Arduino_UNO = serial.Serial('/dev/ttyACM0', 9600) 

Print a message to see that the communication is done:

print("Hello From Arduino!") 

While this executes, the user can enter a command:

while True: command ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required