Category Archives: Knowledge

How to Automate the Turn-On Time of a Deco Light

I own a decorative light for the winter which I controlled using a simple time switch in the past. The time of dusk is continually shifting, so I had to adjust the turn-on time multiple times each year.

This year, I planned to automate the turn-on time. I tried to find a light controlled switch, which automatically enables the decoration on dusk and disable it on dawn. I saw no suitable one.

I ended adding an own switching circuit to the decoration. This article describes how to build an own controller as I did and highlights a few problems you may encounter.

The Issues with Off-the-Shelf Solutions

My requirements for the off-the-shelf solutions were:

  • Setting to adjust the light level.
  • Suppression/identify the light from the decoration.
  • Turn off at a given time or dawn.
  • It shall work at a temperature of -10ºC.

Sadly, I found no simple device, which matched all my requirements. I also noticed, most of these devices had horrible ratings and reviews – mainly because they lacked the described features.

Adjusting the light level seems a straight forward requirement. The brightness, where you like to turn the decoration on is a matter of personal taste. Most devices I found do not let you adjust this.

If the decoration is turned on, it will naturally get brighter outside. If the switch does not compensate for this fact, the light will start to blink. In this case, you had to place the sensor somewhere carefully it is not affected by the light of the decoration.

Most devices just let you select a duration, how long the light stays on. This is the cheapest way to implement a device like this. It makes not much sense though. Because the time of the dusk is moving, the time when the light turns off shifts as well. Useful is either turning the light off at dawn, or at a fixed time.

Then, there I found one device which had all the previous features, but it was rated to indoor use only. The minimum working temperature was 5ºC.

Initial Situation

The initial situation is shown in the following illustration:

A timer connects to the mains plug. The mains adapter of the decoration connects to this timer and converts the 240V AC power to 24V AC. This AC voltage is somehow a standard way to power old, filament lamp based decorations.

Continue reading How to Automate the Turn-On Time of a Deco Light

How to Deal with Badly Written Code

Sadly there is a ton of badly written code out in the wild. Hardware related code, seem to suffer more in this regards. I imagine, many developer in this segment are unwillingly to invest time in quality or are just inexperienced.

Even if you are dedicated in reliable and high quality code, you will probably run into the situation, where you have to use a library with really low standards.

Strategies

There are a number of strategies to deal with this code:

  1. Rewrite the library.
  2. Refactor the code.
  3. Wrap the library.
  4. Wrap the interface.

Rewrite the Library

This is actually the best you can do and it is also the most time-consuming approach. Yet, write the code from scratch will give you several benefits:

  • You will perfectly understand how the original library and the accessed hardware works. Actually you have to understand it, otherwise you are unable to rewrite the code.
  • The new code will be modern, fast, reliable and in your coding style.
  • If you open source the new code, you will give people an alternative and a better example how to implement something the right way.
  • You can also selectively remove unwanted/bloated parts from the original code, which can reduce the overall binary size of the final project.
  • It will also give you the option to implement a proper error handling in the new code.

If you have the time and motivation to rewrite the code, do it!

Refactor the Code

Changing the code, without changing the functionality is called code refactoring. This is a good strategy, a compromise, between rewriting and wrapping. Usually you will just go, line by line, through the original code, modernise it and cleaning it up.

Continue reading How to Deal with Badly Written Code

Make your Code Safe and Readable with Flags

Flags play an important role in embedded software development. Microcontrollers and chips are using registers where single bits or combinations of bits play a big role in the configuration. All the bits and their role are described in the specification, but writing the bits directly in the code would be very confusing and hard to read:

AHBMASK.reg = 0x14 // Huh!?

For this reason it makes sense to write an interface to access the registers of a chip. This interface will define identifiers, in the form of constant values, to build bit combinations to write into the registers.

The Outdated and Bad Approach

Chip manufacturers are well known for their extremely bad and outdated interfaces to access chip registers. Let us have a look at the CMSIS implementation from Atmel. This is part of the interface to access registers in one of the microcontrollers. Please ignore the copyright block, it is just added for legal reasons.

To be fair, CMSIS is a hardware abstraction layer standard defined by ARM. It shall standardise the software interfaces across all Cortex-M products. Therefore Atmel had no choice as to follow this standard.

Do you feel the dust on this code? There are a vast amount of problems caused by this interface.

First the values are defined as macros instead of constants. All this identifiers will clutter the global namespace of your code and make naming conflicts very likely. Way better would be using simple constants like this:

const uint8_t PM_AHBMASK_DSU_Pos = 3;
const uint32_t PM_AHBMASK_DSU = (0x1ul << PM_AHBMASK_DSU_Pos);

This would give the compiler the correct hint about the used data type for the registers. All this constants could be put into a own namespace to prevent any name collisions with the user code.  It would still not prevent incorrect assignments.

I can write and compile the following code, which absolutely makes no sense:

CPUSEL.reg = PM_AHBMASK_DSU; // nonsense!

There will be not even a compiler warning about any problems and this is exactly what makes debugging embedded code very difficult.

Use the C++ Language!

I personally think, many software developers writing C++ code for hardware do not make fully use of the language. One reason could be the lack of support of many language features at the beginning or just the lack of experience.

In the the last 10 years, there was a huge progress in the development of the C++ language. Ignoring this progress would be silly in my opinion. Especially the C++11 standard added lots of useful features to the language.

A good support for the C++11, C++14 or even better for C++17 is very important, especially for embedded software development. Many of the introduced features will improve your code, make it simpler and more readable, without adding any additional byte to your firmware.

The features I describe in this article will not increase the size of the final firmware, they will just affect safety, readability and simplicity of your code. At the end, the optimiser in the compiler will resolve all the code and generate small and compact binaries.

Continue reading Make your Code Safe and Readable with Flags

Soldering for Show

This article is about soldering for show. While usually you solder electronic components to make the best possible connection and keep everything working, this is just to make a great visual result. If you want to take photos from your projects or would like to impress a customer with a nice looking prototype, you have to solder with more care to the detail.

In the next sections I will describe a few things which are worth to think about, using Boldport Project #4 as an example. If you have any questions or suggestions, feel free to add a comments to this article. Also feel free to ask about any other technique I use, about which you would like to see another article.

Equipment

To get great results you need the right tools.

lucky-resistor-4

First, you really need a good soldering station. The brand does not matter, but the performance does. The solder iron should actually transfer the required heat to the solder joint and this in a very consistent and reliable way. For this reason, the temperature sensor should be as close to the tip as possible. I personally use products from the Weller WT Series.

Continue reading Soldering for Show

SMD Components Under the Microscope

Today I took some photos from common SMD components using a microscope. From naked eye, this components look so tiny and fascinating perfect. Looking at them using a microscope, they do not look that perfect anymore.

I cropped the pictures to make the picture better visible at lower resolutions. I always put a scale, which measures 1mm, on each photo. See each picture caption for a description of the component and more details.

All Meggy Jr RGB Colors

I created a complete color table with the impression of all possible 4096 Meggy Jr RGB colors. The green LED is much brighter than the other two LEDs, the first tables used blue as base which does not make much sense. So I created a second set of tables which use the bright green as base.

If you like to create own tables or if you try to create something like a simulator or color lookup, here a ZIP file with all colors as individual PNG images: MeggyJrColorLEDs.zip

MeggyColorGreen_0

Continue reading All Meggy Jr RGB Colors