KuttyPy - Learning Microcontrollers with Python

jithin (~jithinbp)



Beginners to the world of microcontrollers typically start with a development board, and some prgramming tools. Code is written in human readable languages, then compiled into a binary file, which is then flashed into the microcontroller usually via a bootloader for subsequent execution. This journey can be tedious, involving long cycles of compiling-uploading-debugging, and students often choose to skip the most significant part of microcontroller programming - REGISTERS - in favour of high level abstraction functions .

The KuttyPy concept is at its core, a very simple one which uses an ATMEGA32 chip with a bootloader having extra capabilities for reading and writing registers of the microcontroller based on commands received from a connected computer running its IDE. It gives an option to do away with the compile/upload cycle, and instead toggle bits in registers on-the-fly and watch the behaviour of the microcontroller.

It can also monitor input pin states, read ADC values, and send data over the I2C bus, making it an incredibly cheap data acquisition device. For a more organized overview, check out the read the docs page, or the slides attached.

Since it is simply a bootloader derived from Optiboot for the Atmega family of microcontrollers, it can also be compiled and uploaded to Atmega328(Arduino Uno/Nano) boards. The corresponding Graphical layouts are also included with the source code.

enter image description here

The screen recording of the KuttyPy-UI below should give a general Idea.

All values shown are being acquired on-the-fly from a connected kuttypy board. If the user wishes to run their own code in standalone mode, the option to write C code, compile, and upload it using avr-gcc is also present.

KuttyPy User Interface

  • It is worth noting that every click that toggles or invokes a function of the microcontroller is clearly explained by the user interface in terms of the associated register values.

Reading from sensors using the I2C bus

The kuttypy's I2C bus can be used to communicate with simple sensors for measuring magnetic fields, pressure, humidity, angular velocity etc. Protocol for each individual sensor is coded in the Python side of things, and this is merely a transparent communication channel. An example animation below shows readings from an MPU6050.

With the aid of Scipy's fitting functions, all time series data can be fitted against standard functions such as sine, damped sine, and exponential decay. This enables extraction of parameters such as oscillation frequency, amplitudes, and phase shifts which are useful for conducting simple physics experiments.

MPU6050 readings

Talk outline

  • introduction to microcontrollers, and what is a bootloader [ 5 minutes ]
  • The basic command set which facilitates communication between the uC and python [ 3 mins]
  • Python Interface - PyQt5, Scipy, PyQtgraph [ 5 mins ]
  • Register manipulation in Microcontrollers [ 5 mins ]
  • I2C sensors with a short demonstration [Sensor->microcontroller->PySerial->Pyqtgraph/Scipy analytics [ 10 mins ]
  • Internationalisation [ 1 min ]
  • Packaging [ 1 min ]

Python Tools used for this development

  • PyQt5 - Graphical user interface
  • PyQtgraph - for all thos fancy plots
  • Numpy + Scipy- All data analysis such as curve fitting
  • pyserial

Learning Goals

Sample Python Code

# Read value from Analog to Digital converter(ADC) channel 0 (PA0)
from kuttyPy import *
setReg('ADMUX', (1<<6) | 0) #REF_AVCC | Channel 0
setReg('ADCSRA', 196)  #Set ADC conversion speeds, and enable it. Refer to the ATMEGA32 datasheet for details.
cl = getReg('ADCL')  #LSB 8 bits
ch = getReg('ADCH')  # MSB 2 bits
print( (ch<<8)|cl )  # Combine the two to make a 10 bit number, and print to the screen


Familiarity with microcontrollers/arduino.

Video URL:


Content URLs:

Speaker Info:

I have a long association with developing scientific tools for data acquisition and science experiments. Ranging from the development of a data acquisition software(PyQt,Python-GPIB,Pyqtgraph) for Point Contact Spectroscopy during my Masters thesis work at IISER Mohali, to the development of a range of standalone instruments which use Python to interact with the real-world.

I have conducted workshops and delivered talks on Python with Microcontrollers(KuttyPy) at Scipy 2019, Scipy 2020, makerville.io, and fossunited. I also contribute to the ExpEYES project

Personal Website: http://jithinbp.in/ Company Website: https://csparkresearch.in/

I am currently contributing to three packages on Debian/Ubuntu : eyes17, SEELablet, and KuttyPy . Repositories I contribute to: https://github.com/csparkresearch/kuttypy-gui -> https://kuttypy.readthedocs.io/en/latest/ https://github.com/expeyes/expeyes-programs/tree/master/eyes17 https://github.com/csparkresearch/eyes-online https://github.com/csparkresearch/cnspec -> https://csparkresearch.in/cnspec . A nuclear data analysis software. It has been open sourced, and will soon enter Debian.

Section: Embedded Python and IOT
Type: Talks
Target Audience: Beginner
Last Updated: