Anaconda is a free and open-source distribution of the Python (and R) programming language . Remember, Python is one of the most widely used languages ​​today and allows you to create scripts, as it is an interpreted language. Therefore, it relies on the Python interpreter for the machine to understand and process it. In contrast, compiled languages ​​are translated into a binary file that the machine can directly understand without an intermediary.
Well, Anaconda is widely used in education and machine learning. But you can also use it to control an Arduino board . Yes, that's right, you could be using the Python programming language to create your own programs to control Arduino easily and without any problems. Here we'll explain how to do it step by step…
What is Anaconda
Well, you already know it's a Python distribution , and therefore shares features with official Python. But it offers some advantages over Python, besides 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 features 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

To use Python to control Arduino, you'll need an API . Once you have Anaconda installed on your operating system, you can easily install the API . It's as simple as invoking Anaconda from the command line. Your shell prompt will then change to the Anaconda prompt, and you can use the following command, and conda will take care of everything:
conda install -c auto arduino-python
Once you've installed arduino-python, you can start using it with your Arduino projects . But first, you need 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 Python version you'll be using. For example:
conda create --name arduino python=3.7
The "arduino" virtual environment is now created for use with Python version 3.7. The next step is to activate it :
conda activate arduino
Once active, keep this terminal window open; don't close it, as you'll need it later. As you can see, the prompt is now (arduino)> so you can begin. It would be a good idea to install some packages you'll need now, such as PySerial for communication with the Arduino board. To do this, simply use the following command from the same prompt:
conda install pyserial
Okay, now download and install the Arduino IDE if you don't already have it. If you do, then you can move on to the next step, which is writing the Python code to control your Arduino project. You can write a simple program or use the examples that come with the Arduino IDE to try it out.
For 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, just as you would on a Raspberry Pi with the GPIO pins, to alter the output. For example, in this case, you can turn the LED on or off as you wish with a simple Python code.
To do this, you can return to your terminal window and within the Anaconda prompt do the following:
> 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) >
This will allow the `ser.write` function to turn the LED (L) on or off as desired . Then `ser.close()` ends and `exit()` exits. This is how you can control your Arduino projects with Python. Obviously, this is just one example, but you can imagine many more cases…
You can also create .py files with Python scripts so you can run them at any time without having to do so directly from the Anaconda prompt. For example, this one:
# 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, right? And if you're already familiar with Python, this will be even easier. You'll just need to run your .py file and interact with the program's interactive options to enter commands to turn the program on, off, or exit.
Table of Contents