How to read and write to Arduino using Python

In this tutorial you will learn how to read and write to Arduino Pins using Python programming language. A complete python program to read switch and then turn on a LED is provided at the end as an example.

In this Arduino programming with python tutorial we show how to read and write to Arduino using Python. Learning how to use programming language to write data to pins and/or reading data from pins is the fundamental step in learning how to write program code for Arduino.

First we illustrate how to write data to digital pin and then we show how to read data from digital pin. At the end we show a complete python program code, physical hardware and video to illustrate how to read and write to Arduino using Python programming language with a LED and Switch example.

We assume here that you have already uploaded the StandardFirmata program into Arduino UNO and installed the pySerial and pyFirmata libraries. If not then this tutorial Programming Arduino using Python shows you how to do it.

Writing to digital Pins in Arduino using Python

First import the pyfirmata module,

Then create the board object using the firmata.Arduino() function.

Once you have the board object you can use two different ways to write(or read) data to the digital pins. Consider that we want to use the digital pin 6 and we want to send logic High or 1 to the pin.

The first method is to use the digital[6].write(1) method.

The second method is to use the get_pin(‘d:6:o’).write(1) method as follows,

Here, we have use the get_in(pin_def) method and passed into it the pin mode, d for digital, the pin number which is 6 and the pin input/output mode which is o for output.

This get_pin() thus allows specifying the pin functions. To use analog pins we would use ‘a’ analog pin Pin number, for input mode we would use ‘i’ and ‘p’ for pwm (Pulse-width modulation).

Optimally we could have declared a led variable like this,

Then used the write() function,

Similarly if you want to write LOW value to the digital pins you should use,

 

Reading from digital Pins in Arduino using Python

Next we show how to read values from digital pins in arduino using python programming language. The steps are similar to the writing values to digital pins.

First import the pyfirmata module,

Then again create the board object using the firmata.Arduino() function.

Now once you two ways to write command to read values from the digital pins. Now unlike in writing to digital pin 6, we will read value on the digital pin 2. Say a switch is connected to digital pin 2. Then one way to read values from pin 2 is to use digital[2].read() method.

Unlike in write() function, no argument is required for read() function.

The second method is to use the get_pin(‘d:6:i’).read() method as follows,

Here we used the get_in(pin_def) method. The difference between here is using i for input because we are reading values.

Optimally we could have declared a sw variable(sw standing for switch) like this,

Then used the read() function,

So these are ways with which you can read and write to Arduino Pins using Python.

Example of How to read and write to Arduino using Python

Next we will write a complete python program code for reading a switch state connected to pin 2 and turn on/off a LED connected to pin 6 of Arduino UNO. This is same trick we demonstrated for reading switch state when using just arduino(see) How to read switch input in Arduino and using simulink(see Arduino Simulink Tutorial 2 – Reading Switch).

For now consider the following circuit.

circuit diagram for reading and writing using python arduino

The actual physical hardware circuit set up is shown below.

hardware set up to read and write data using python arduino

Now what we want is this. As soon as the switch is close we connect the switch to 5V to pin 2 and this means pin 2 is High state. If this pin 2 is high we want to show that on the LED by turning on the LED. To do this we utilize the above read and write examples.

The following is the complete code to read the switch state and turn on the LED accordingly.

Now watch the following video demonstration to see how it works in real time.

So in this way you can read and write to Arduino digital pins using python programming language.

If you want more Arduino tutorials see Arduino Matlab Simulink Tutorials.

[ebook_store ebook_id=”1142″]

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

single led connection for testing firmata

Blinking LED python program for Arduino

Reading analog voltage with Python using Arduino

Reading analog voltage with Python using Arduino