Counting time with Arduino (basic clock)

 Author:   Posted on:    No comments
When it comes to counting time using an Arduino or other MCU platform, the use of a RTC (real time clock) circuit is highly recommended. These devices are cheap, have good accuracy and keep counting time even when the main MCU is not powered. They run from a small battery and draw low current. There are quite a lot of Arduino libraries that deal with time and make the interaction with a time source easy. One of these is the Time library by Paul Stoffregen. It is a well written library with internal time based on standard Unix time time_t (number of seconds passed since Jan 1, 1970).

But I wanted something even more basic. I wrote a 50 lines of code function that increments seconds variable each time it is called. When seconds overflow (reach 60), it increments minutes variable and resets seconds. And so on. Only when a time variable changes, it is printed on the output device (16x2 LCD in this case). Around this function I added code that turns the Arduino into a common clock. As a prototype as used the LCD and keypad shield fitted to an Arduino Uno compatible board. The code should compile on any other development board because it doesn't use specific functions or libraries. You will have to adjust LCD pin configuration and keypad buttons (require modification if using buttons connected to digital input pins instead of the analog keypad).

Counting time with Arduino (basic clock)

Truly LCD front panel: the backlight (2)

 Author:   Posted on:    No comments
Here is the follow-up of the post where I described how I took out the front panel of a router (yes, a router) and found a way to interface it with Arduino or other development board. It should be noted that the front panel electronics use 3.3V levels, therefore the popular 5V Arduino boards cannot drive the front panel. Using level shifters would complicate things and increase the possibility of something going wrong, so I ended up using a 3.3V STM32 blue pill development board. This is programmed from Arduino IDE, so the code I write is compatible with Arduino development boards.

While I was sampling various pins of the front panel connector with a logic analyzer, I noticed a strange protocol on pin 18. I was able to trace the PCB track from pin 18 near an area that seemed like a DC-DC converter. It directly drove an integrated circuit marked T43. Searching for it revealed some LDO linear voltage regulators, but this was not the case. Pin 18 carried a digital protocol that would be of no use for an ordinary voltage regulator. But without information I could only write code that would mimic the protocol I sampled. Things changed once the GPL source code has been made public. The signal on pin 18 had a meaning. It was necessary to turn on/off and dim the backlight. Upon powering the front panel on the breadboard, the backlight stayed off. You can turn it on by setting pin 18 high but if you want to adjust its level you must send two bytes using a custom serial protocol. Before getting to the code let’s see an overview of the pins and connections on the breadboard.

Front panel connector adapter on breadboard
Front panel connector adapter on breadboard

Router's LCD and keypad interfaced to Arduino (1)

 Author:   Posted on:    No comments
A router with display is not something you see everyday. That’s why when I saw two such routers that were discarded I bought them. At that time I had absolutely no idea if I could install an open source firmware on them (such as OpenWrt). I didn’t even know what type of display do they use. Currently, there is no way of installing a 3rd party firmware on those devices. But the front panel of the router can be interfaced to a microcontroller (only a specific hardware version).

The devices I’m talking about are SerComm SHG1500 routers, used for ADSL internet. They are based on Broadcom BCM6361 SoC and, although this platform is supported by OpenWrt, a specific build for this device or a way to upgrade firmware isn’t known. So I gave up this idea. But upon opening the case, the front panel with LCD display looked like a module that could be used for my projects. The LCD is color TFT, 2.8” size. Next to it there is a capacitive keypad with 5 keys. Front panel plugs into main board using a 2x15 pins, 1.27 mm pitch connector. It seemed good enough to start gathering information.

SerComm SHG1500 front panel with LCD and capacitive keypad
SerComm SHG1500 front panel with LCD and capacitive keypad

STM32Cube code initialization for “blue pill”

 Author:   Posted on:    1 comment
Lately I’ve playing around with the STM32F103 development board known as “blue pill”. Developing software for it is not as easy as for Arduino boards. The MCU contains a 32-bit ARM CPU. I have previously tried to write software for this board using HAL library, but I didn’t get the most out of it because I found the programming model rather complicated.

One of the methods to develop software for this MCU is to use ST HAL library (which uses a higher level API than other libraries for this MCU). I chose the Eclipse IDE with a set of plugins for STM32 family. There is an easier way to get the SDK and IDE with the toolchain called System Workbench for STM32 (recommended by ST too). This is the download directory where you can find all releases for the major operating systems. But before creating a blank project in SW4STM32, you should know that there is a tool which can create this project for you. Not quite blank, as you will configure the MCU with a graphical tool in a step-by-step process.

STM32Cube code initialization for “blue pill”

STM32 “blue pill” easy development with Mbed

 Author:   Posted on:    No comments
The "blue pill" is an STM32F103 based development board. Although it is less popular, the board is cheaper than an Arduino Nano. More than that, STM32F103 is a device with Cortex-M3 ARM CPU that runs at 72 MHz, 20 kB of RAM and 64 or 128 kB of flash memory. The microcontroller (MCU) has USB port, two serial ports, 16 bit PWM pins and 12 bit ADC pins. It runs at 3.3V, but some of its pins are 5V tolerant.

I tried to program this development board using both Arduino IDE and STM32 HAL, but I wasn’t quite satisfied. Arduino framework is simplified and does not take advantage of platform’s features, while HAL was quite difficult for me. Using HAL in Eclipse come with another disadvantage: direct uploading of the binary in flash didn’t work, so I had to use ST-Link tools to upload it, outside of Eclipse. Recently I heard of PlatformIO IDE. This is a development environment supplied as Visual Studio Code or Atom plugin. One of its great advantages is the support for more than 500 development boards! Although VS Code and Atom are cross platform software, they are not at all lightweight, so you’ll need rather good hardware to run them smoothly.

Another great feature of PlatformIO is that for a development board you have multiple framework options. You can program the bluepill using Arduino API, just as you would do in Arduino IDE. Or you can program it using STM32Cube API. But, there is also Mbed OS framework, which I found it to be quite easy to develop.

STM32 “blue pill” easy development with Mbed

How to Count Frequency with Arduino

 Author:   Posted on:    No comments
Counting frequency using an Arduino seemed like an easy task. But most people like to do it the easy, but wrong way: using pulseIn to measure width of a pulse. This limits the maximum frequency that can be measured to about 50 kHz. Besides that, the function samples only a cycle of the signal.

A good way of measuring frequency is by counting input signal transitions that happen in a specific amount of time. This requires knowledge of timers and interrupts. The method is more difficult to implement and to do it right you need to set some registers.

This has been done before and although it was hard to find, I discovered code that can count frequencies up to 8 MHz if the input signal has a duty cycle of 50%. The only drawback is that frequency input pin is fixed to digital pin 5. The upper range is however not limited to only a few MHz. With some extra hardware (a prescaler IC) frequencies of hundreds of MHz can be measured with enough accuracy.
How to Count Frequency with Arduino