Home » A look at an Arduino and Python library

A look at an Arduino and Python library

Oracle Java Certification
Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Programmer II [1Z0-816] Practice Tests
Spring Framework Basics Video Course
1 Year Subscription

I am a big of the arduino, that i s one of the reasons I also have an arduino based site at http://arduinolearning.com

In this article we will look at one of several libraries that can be used with Python to send commands to your Arduino.

Here is the details of the Arduino-Python3 Command library

The Arduino-Python3 Command API is a lightweight Python library for communicating with Arduino microcontroller boards from a connected computer using standard serial IO, either over a physical wire or wirelessly. It is written using a custom protocol, similar to Firmata.

This allows a user to quickly prototype programs for Arduino using Python code, or to simply read/control/troubleshoot/experiment with hardware connected to an Arduino board without ever having to recompile and reload sketches to the board itself.

Method names within the Arduino-Python3 Command API are designed to be as close as possible to their Arduino programming language counterparts

Requirements

You will need an Arduino, Python 3.7 and Pyserial 2.6 or higher

Installation

pip install arduino-python3

Setup:

The setup is very easy to do

  1. Verify that your Arduino board communicates at the baud rate specified in the setup() function (line 407) in prototype.ino. This is set to 115200
  2. Load the prototype.ino sketch onto your Arduino board, using the Arduino IDE.
  3. Set up some kind of serial I/O communication between the Arduino board and your computer (via physical USB cable, Bluetooth, xbee, etc. + associated drivers)
  4. Add from Arduino import Arduino into your python script to communicate with your Arduino and then use the available commands

Methods

Digital I/O

  • Arduino.digitalWrite(pin_number, state) turn digital pin on/off
  • Arduino.digitalRead(pin_number) read state of a digital pin
  • Arduino.pinMode(pin_number, io_mode) set pin I/O mode
  • Arduino.pulseIn(pin_number, state) measures a pulse
  • Arduino.pulseIn_set(pin_number, state) measures a pulse, with preconditioning

Analog I/O

  • Arduino.analogRead(pin_number) returns the analog value
  • Arduino.analogWrite(pin_number, value) sets the analog value

Shift Register

  • Arduino.shiftIn(dataPin, clockPin, bitOrder) shift a byte in and returns it
  • Arduino.shiftOut(dataPin, clockPin, bitOrder, value) shift the given byte out

bitOrder should be either "MSBFIRST" or "LSBFIRST"

Servo Library Functionality Support is included for up to 8 servos.

  • Arduino.Servos.attach(pin, min=544, max=2400) Create servo instance. Only 8 servos can be used at one time.
  • Arduino.Servos.read(pin) Returns the angle of the servo attached to the specified pin
  • Arduino.Servos.write(pin, angle) Move an attached servo on a pin to a specified angle
  • Arduino.Servos.writeMicroseconds(pin, uS) Write a value in microseconds to the servo on a specified pin
  • Arduino.Servos.detach(pin) Detaches the servo on the specified pin

Software Serial Functionality

  • Arduino.SoftwareSerial.begin(ss_rxPin, ss_txPin, ss_device_baud) initialize software serial device on specified pins. Only one software serial device can be used at a time. Existing software serial instance will be overwritten by calling this method, both in Python and on the Arduino board.
  • Arduino.SoftwareSerial.write(data) send data using the Arduino ‘write' function to the existing software serial connection.
  • Arduino.SoftwareSerial.read() returns one byte from the existing software serial connection

EEPROM

  • Arduino.EEPROM.read(address) reads a byte from the EEPROM
  • Arduino.EEPROM.write(address, value) writes a byte to the EEPROM
  • Arduino.EEPROM.size() returns size of the EEPROM

Misc

  • Arduino.close() closes serial connection to the Arduino.

Examples

These examples are for an Arduino mega

The first flashes the on board LED and also reads the values of the analog in 0,2,4 and 15

from Arduino import Arduino
import time

board = Arduino() # plugged in via USB, serial com at rate 115200
board.pinMode(13, "OUTPUT")


board.digitalWrite(13, "LOW")
time.sleep(1)
board.digitalWrite(13, "HIGH")
time.sleep(1)


val0=board.analogRead(0)
print("Pin 0 : " + str(val0))
val2=board.analogRead(2)
print("Pin 2 : " + str(val2))
val4=board.analogRead(4)
print("Pin 4 : " + str(val4))
val15=board.analogRead(15)
print("Pin 15 : " + str(val15))

This example loops through the analog pins

from Arduino import Arduino
import time

board = Arduino() # plugged in via USB, serial com at rate 115200
board.pinMode(13, "OUTPUT")


board.digitalWrite(13, "LOW")
time.sleep(1)
board.digitalWrite(13, "HIGH")
time.sleep(1)

i = 0
while i < 16:
  val=board.analogRead(i)
  print("PIn number " + str(i) + ": " + str(val))
  time.sleep(0.1)
  i += 1

This example sets 6 pins as ouputs and flashes them on and off

from Arduino import Arduino
import time

board = Arduino() # plugged in via USB, serial com at rate 115200
board.pinMode(42, "OUTPUT")
board.pinMode(44, "OUTPUT")
board.pinMode(46, "OUTPUT")
board.pinMode(48, "OUTPUT")
board.pinMode(50, "OUTPUT")
board.pinMode(52, "OUTPUT")

board.digitalWrite(42, "HIGH")
board.digitalWrite(44, "HIGH")
board.digitalWrite(46, "HIGH")
board.digitalWrite(48, "HIGH")
board.digitalWrite(50, "HIGH")
board.digitalWrite(52, "HIGH")
time.sleep(1)
board.digitalWrite(42, "LOW")
board.digitalWrite(44, "LOW")
board.digitalWrite(46, "LOW")
board.digitalWrite(48, "LOW")
board.digitalWrite(50, "LOW")
board.digitalWrite(52, "LOW")
time.sleep(1)

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More