In this tutorial you will learn how to read analog voltage signal using Arduino with python programming language. To read in analog voltage signal we will use a potentiometer connected to the analog pin A0 of the arduino uno board. By varying the potentiometer the analog pin A0 will sense different analog voltage. This is internally converted to digital sampled signal by Arduino. Then we can print out the analog signal values.
You may also want to read How to read analog signal with Arduino where we have used Arduino IDE and the program is written in native arduino programming language. Also you might find the tutorial Real-Time data acquisition from Arduino to Matlab useful if you want to plot analog voltage signal after you have read it.
Hardware setup for Reading analog voltage with Python using Arduino
The following is the schematic diagram for connecting 10KOhm potentiometer with the Arduino pin A0.
The actual hardware is shown below.
As you can see the center pin(yellow wire) is connected to the analog pin A0. The ground(black) and the red wire are connected to the ground and 5V supply on the arduino board via the breadboard.
Python Programming for reading analog signal using Arduino
In order to program Arduino using python programming language, you should have the pyFirmata and pyserial library installed. If you don’t know how to setup python for programming Arduino see the tutorial Programming Arduino using Python.
Then in your Python program editor type the following python program. Here spyder IDE is used.
from pyfirmata import Arduino, util
from time import sleep
board = Arduino('COM3')
it = util.Iterator(board)
a0 = board.get_pin('a:0:i')
v = a0.read()
Video demonstration of Reading analog voltage with Python using Arduino
The following video demonstrate this tutorial.
So in this way you can read analog voltage signal using Python programming language and Arduino.