Reading analog sensor values with Atmega328p, sending them to PC via bluetooth + plot and save on a .csv using Python

An analog signal is read by the Atmega328p, then sent to the HC-05 Bluetooth module via UART. On the PC, the incoming data is processed by a Python script and plotted using matplotlib.

On another approach, the values are continuously stored to a .csv file and read + plotted by a second script.

Schematic

The programming was done in Microchip Studio, using an USBasp programmer and AVRDude Assistant GUI.

Code AVR

Python code approach 1

The baudrate of the HC-05 Bluetooth module has to be set correctly via AT-commands, e.g. to 115200. Default password is something like ‘0000’ or ‘1234’ when connecting to windows. The used COM-Port can be identified in the device manager.

Animated plot using Python matplotlib.

The readline() method is slow and the limiting factor here. So depending on how fast the 328p transmits the data, there could be a certain delay until changes in the displayed plot are visible after turning the potentiometer.

Python code approach 2

Now implementing a faster way of reading the serial data that I found on github.

The above minimum example works great and prints the ADC values super fast and very reliable.

However, upon adding the remaining code for plotting, problems start to occur.

The ADC values sometimes jumps significantly, even when e.g. the ADC0 Port is connected to the fixed voltage of 3.3V. Also, sometimes an empty byte is read and therefore throws a ValueError when trying to convert it to int. Both things didn’t happen with approach 1.

Continuously save to .csv and live-plot from there

Two separate scripts are used, the first reads the serial data and writes it to a .csv file, the second script reads from the continuously updated .csv file and plots it. The scripts can be manually started via the terminal, or a by a short bash script that executes them both.

First script reading the serial data and writing it to the .csv file. (CTRL+C to stop the process)

The main script reads the data from the .csv and plots it.

Conclusion

  • I tried the same procedure with the HM-10 Bluetooth module, but windows won’t assign an COM-Port. Apparently, windows does not support pairing with BLE modules. Thanks Obama.
  • The readline() method is slow. Like really slow. There is a faster alternative from ‘skoehler’ here https://github.com/pyserial/pyserial/issues/216
  • For me, the faster readline() mentioned above works great in a standalone setup. But as soon as I add all the plotting code around it, it doesn’t work properly anymore. E.g. Values jumping around and many empty bytes received and ValueErrors when trying to convert them to int and so on.
  • I somehow wanted to bypass the problem by writing the incoming data directly into a .csv file and then create a plot that continuously reads from that very same .csv file. This has two advantages: It stores the measured data and also verifies the data at the same time, since the data for the continuously updating plot is read from that .csv file. But for reading the serial data again some kind of Python serial.read method had to be used and the code with the faster alternative started to deliver weird values upon speed increase as mentioned above.