We Code, built, and share our thoughts to do chemistry

Thursday, 13 February 2025

Devlog - Building Potentiostat: Microcontrollers

Potentiostat cannot run by itself. We need to configure parameters such as voltage range, scan rate, mode (linear sweep voltammetry, cyclic voltammetry), number of measurements per time, etc. It's going to be difficult to configure all of these parameters manually. Therefore, we need a controller to control and monitor the measurements.

For the microcontroller selection for potentiostat applications, there are several considerations we take into account, voltage tolerance, type  and amount of peripherals it provides. This would make sure that we can utilise and maximise the performance of the device.

First thing first, the microcontroller, there are many selections of microcontroller we select from such as Arduino, ESP32, ESP8266, Raspberry pi pico, STM32, even the RISC-V's CH32V. Arduino provides rich libraries and high digital voltage level which can be useful for potentiostat voltage range. However arduino has several limitations such as the number of timers and memory. Despite some works managed to push the limits of arduino for this application(link), We believe having more flexibility and less constraints in the project would be more beneficial during the development process. This goes the same with ESP-based microcontrollers. Even though they provide communication modules such as WiFi or bluetooth, the logic level voltage is 3.3V that might require additional peripherals such as level shifter in order to communicate with 5V based modules. Raspberry pi pico offers better performance, even with dual core and streamlined software development. However, the capability to tolerate 5V is a deal-breaker. Therefore the choices are either STM32-based microcontrollers or WCH32-based microcontrollers.

Throughout the development, We have been using both STM32 and WCH32 based microcontrollers. To be more precise, We've been using it back-and-forth for this project. Recently, We used both STM32G0-based and CH32V307-based microcontrollers. We're not sure either skill issue or hardware reliability, STM32G0 provides better hardware reliability than CH32V307 microcontrollers. Nevertheless, both provide such an experience in developing projects using various microcontrollers.

In order to optimise the use of microcontrollers for potentiostat applications,  several microcontrollers should be used. Here are several peripherals We use for the potentiostat.


PC-Device communications: UART

In order to configure and give command to the microcontroller, we need to be able to communicate with it. One of the simplest ways is to communicate through Universal Asynchronous Receiver-Transmitter(UART). It's simple and fast enough to communicate for such a simple computer-device communication. What's better in using either STM32 or CH32V-based microcontrollers we have that they both provide UART interrupt. Interrupt, as the name suggests, would intervene the current code, run the interrupt code, and return the intervened code. UART interrupt service routine (ISR) will take place when a particular condition is achieved. For example, when we provide a buffer containing several characters, when the number of characters has been achieved or past the number of available buffer, the interrupt code (handler) would take place. The use of interrupt also makes the device experience work more smoothly.

USB communication would be more desirable. However, we'll focus on completing it and making it work optimally first. Once, it's achieved we can think of better improvements.

Analogs: DAC and ADC

Since potentiostat involves heavily analog signals, analog peripherals such as digital-to-analog converter (DAC) and analog-to-digital converter (ADC) would absolutely be a must. DAC is used to generate analog signals for potentiostat. Even if we can use filters to generate similar signals, DAC provides more reliable analog signals. Therefore a microcontroller with a DAC is a must. We can still use external DAC though for better resolution and wider voltage range, though. However, somehow the DAC is conflicted with the external ADC we also use which creates an headache. Therefore, at the moment we're sticking to use internal DAC from the microcontrollers. Furthermore, 12-bit resolution is still more than adequate.

Besides the analog generator, we would also need to read the analog signal, which is why the ADC comes in. Built-in microcontroller has already 12-bit resolution. However, I use external ADC with 16-bit resolutions providing more precise reading and wider voltage range. Wider voltage range is important because the voltage generated might be over what it's designed to be. Even if we use zener diode to protect the overvoltage, It's good to have another line of protection.

Right place and the right time: Timer Interrupts

After the peripherals are all set, the question is when do we set the voltage or read the voltage? That's when we implement timers. There are many types of timer. In this case, Once the timer is start, it would intervene the current code and run the interrupt code (handler) after certain period and repeat until we tell them to stop. In our case, we use separate timers for generating and reading analog signals. We do it to prevent deep if-else conditions on handler code. It might add more complexity in which the timers have to be set and configured separately. The microcontroller has multiple number of timers which we can utilise. Moreover, preventing deeper if-else conditions in handler code can make the code run more reliably during interrupt service routine. We use 100ms interval for both signal generation and voltage reading with 50ms difference. Therefore, when the timer for signal generation is start, timer for voltage reading would be set 50ms later. This ensures the voltage settled and accurately read by the ADC.

No comments:

Post a Comment

Our potentiostat circuit design approach compared to a glucometer

Previously, we rebuilt a potentiostat circuit to improve our design. We built it on a breadboard as we can experiment with it. You can watch...