Anaconda (Python) for your Arduino

Anaconda logo

Anaconda is a free and open distribution of the Python programming language (and R). Remember, one of the most widely used languages ​​today and that allows you to create scripts, since it is an interpreted language. Therefore, it depends on the Python interpreter so that the machine can understand and process it. In contrast, compiled languages ​​are translated into a binary that can be directly understood by the machine without an intermediary.

Well, Anaconda It is widely used in the field of education and machine learning. But also what you can use to control Arduino board. Yes, exactly, you could be using the Python programming language to create your programs to control Arduino without any problem and in a simple way. Here we are going to explain how to do it step by step ...

What is Anaconda

Well, you already know what a python distribution is, and therefore shares characteristics with the official Python. But it offers some advantages over Python, apart from being cross-platform and open source, such as:

  • Lets install and manage packages, dependencies and environments for data science in a simple way.
  • It allows the use of various programming environments or IDEs, among them also you can use it with Arduino, Raspberry Pi, etc.
  • It has tools such as numpy, Numba, Dask, Bokeh, Datashader, Holoviews, Matplotlib, etc., to analyze and visualize data.
  • Allows compile python into machine code instead of being interpreted for faster execution. That is, it would become a compiled language.
  • Allows write more complex, high-performance, and even portable programs between platforms to take your projects wherever you want.

More information - Install Anaconda

API to use with Arduino

Arduino and Python logo

For, to use Python to control Arduino you will need an API. Once you have Anaconda installed on your operating system, you can install the API in a very simple way. It is as simple as invoking anaconda from the command line, at that moment your shell prompt will change to the Anaconda prompt, and inside you can use the following command and conda will take care of everything:

conda install -c auto arduino-python

Once arduino-python is installed, already you can start using it with your Arduino projects. But first, you have to prepare the environment to create a new virtual environment for your first Arduino project. To do this, within the Anaconda prompt you can use the following command specifying a name and the version of the Python language that you are going to use. For example:

conda create --name arduino python=3.7

Now the virtual environment "arduino" is already created for use with Python version 3.7. It next is to activate it:

conda activate arduino

Once active, keep this terminal window, do not close it, as you will use it later. How can you see that the prompt is now (arduino)> so you can get started. It would be convenient that you install now some packages that you will need, such as PySerial for communication with the Arduino board. To do this, simply use from that same prompt:

conda install pyserial

Alright now download and install Arduino IDE If you didn't already have it, if you did, then you can move on to the next step, which is to write the code in Python to control your Arduino project. You can write a simple program or use the examples that come with the Arduino IDE to test.

By example, go to Arduino IDE> File> Examples> Communication> PhysicalPixel and upload it to your Arduino board connected to your PC. Once the sketch is working, you can use Python as you would on the Raspberry Pi board with the GPIOs to alter the result. For example, in this case to turn the LED on or off at will with a simple Python code.

For this, you can go back to your terminal window and into the Anaconda prompt Do the next:

> conda activate arduino
(arduino) > python

Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import serial
>>> ser = serial.Serial('COM4', 9800, timeout=1)
>>> ser.write(b'H')
>>> ser.write(b'L')
>>> ser.close()
>>> exit()
(arduino) >

That will make the ser.write function can turn off (L) or turn on the LED (H) at will. Then ser.close () ends and exit () exits. So you can control your Arduino projects with Python. Obviously this is just an example, but you can imagine many more cases ...

You can also create .py files with Python scripts to be able to execute them at any time without having to do it directly from the anaconda prompt. For example this other:

# Ejemplo titileo_LED.py

import serial
import time

# Define el puerto serie
# Debes comprobar desde el gestor de dispositivos de tu sistma operativo a qué puerto se corresponde el USB de la placa Arduino, en Windows sería COM4 en nuestro caso
ser = serial.Serial('COM4', 9600)

def led_on_off():
    user_input = input("\n Elige comando: encendido / apagado / quitar : ")
    if user_input =="encendido":
        print("LED está encendido...")
        time.sleep(0.1) 
        ser.write(b'H') 
        led_on_off()
    elif user_input =="apagado":
        print("LED está apagado...")
        time.sleep(0.1)
        ser.write(b'L')
        led_on_off()
    elif user_input =="quitar" or user_input == "q":
        print("Salir del programa")
        time.sleep(0.1)
        ser.write(b'L')
        ser.close()
    else:
        print("Comando no válido")
        led_on_off()

time.sleep(2) # Espera a que el puerto serie inicialice 

led_on_off()

Simple truth? Furthermore, if you already knew the Python language, all this will be much easier for you. You will only have to run your .py and interact with the interactive options of this program to enter commands to turn on, turn off or exit the program in this case.


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.