Among the cheap gadgetry that constantly spews forth from the spawning pits of consumer electronics, sometimes you can find a gem. The W1209 is an interesting board which is designed to act as a thermostat which can also switch a relay when certain temperatures are reached. It is used in everything from rice cookers to yogurt machines to people who want their A/C to only turn on when it’s hot out. Originally they shipped with an aging but fairly capable STM8S003F3 microcontroller, and they cost less than $1.50 each.
Even so, their popularity has inspired the usual imperfect cloning process, so chances are that a board you buy from ebay, Aliexpress, Taobao, or Amazon today will need some touching up before you will be able to reprogram it. In this tutorial, we will replace the microcontroller and add a few missing capacitor/resistors. Then we’ll upload a simple test program to test that the microcontroller and board both work. So here is an example of what is probably the worst possible knock-off that you could fear to get, with missing or faulty parts highlighted:
Well, that’s life. You try your best, and then someone comes along and kicks you in the teeth. But let’s make some lemonade and take the opportunity to learn about fixing cheap-o knock-off circuit boards!
A note before we start: unfortunately, as of the time of writing, the dirt-cheap STM8S003F3 microcontrollers are out of stock just about everywhere. Mouser reports factory lead times of 34 flippin’ weeks, so perhaps it is not surprising that cheap generic boards containing genuine parts are getting to be uncommon. It looks like you might still be able to find STM8S103F3 chips if you’re quick – check Octopart. They are pin-compatible, but a bit more expensive because they have some extra features. Another option is to buy some cheap STM8S103F3 development ‘blue boards’ and steal their MCU cores; there’s probably large stocks of them backed up, and they cost about $0.75-$1.50 last time I ordered a couple dozen, depending on where you order from. Check eBay, Taobao, AliExpress, etc:
Step 1: Replacing Incorrect and Missing Parts
So, first take a look at your W1209 board. If it already has an STM8 microcontroller on the board and all of the empty pads circled in the picture above have parts on them, congratulations! You can skip most or all of these steps and start programming the board! Spare a thought for the rest of us.
Step 1a: Replace the Microcontroller.
The rest of us will start by replacing the microcontroller and making sure that the
C5 spot is populated with a 1-microFarad ceramic capacitor. You can see that in my board, the cheap-o “nuvotron” chip doesn’t need an external capacitor and leaves the pad unpopulated. This is not what the pictures in the listing I ordered from showed, so be sure to check your listing’s reviews for actual pictures of the products that people received. Lesson learned:
To remove the microcontroller, apply lots of flux to both lines of pins – I used a flux pen made by SRA for this rework, but you may have your own preferences. Once the flux is applied, just take a heat gun and apply low heat to the corner of the board until you can see the metal starting to move around near the pins. When that happens, apply heat to the entire chip and simplly pull it off the board with some tweezers. Be careful not to burn yourself with the heat gun, and be aware that the board itself will be hot to the touch for a little bit afterwards.
A word of caution: DO NOT take your heat gun and apply a high heat setting to the entire board! You’ll notice that the microcontroller is located near a 3-digit LED display and a 12V relay, and both of those parts have plastic shells with low melting points. If you are…overenthusiastic…in your heating, you might accidentally melt them. Don’t feel bad; I did it once or twice figuring this process out, and it’s good to learn about these sorts of things on boards which cost less than a cup of coffee.
It does still seem to work, but would you trust that poor franken-board to switch a high-power load?
Step 1b: Check that it Works
Once you’ve installed the genuine STM8 chip, it’s a good idea to check that the chip works and is correctly connected to the board. The remainder of our repair work is mostly just for safety and signal stability, so we can run a quick diagnostic now to check if we can use the board at all. If it’s broken, why bother wasting time with the rest of the parts?
So if you’re lucky, the four programming pins will be empty through-holes at a 0.1″ pitch. But if you got unlucky like me, they might be filled in with solder. Use a solder sucker to empty them, and optionally attach a 4-pin header for programming. The connections to an ‘ST-Link’ USB module are as shown below:
Connect your programmer, and check that the chip works by flashing its factory defaults:
echo "00 00 ff 00 ff 00 ff 00 ff 00 ff" | xxd -r -p > factory_defaults.bin stm8flash -c stlinkv2 -p stm8s003f3 -s opt -w factory_defaults.bin
-p argument with
stm8s103f3 if you’re using one of those chips instead; they’re pin-compatible and should work fine. If the chip is connected correctly, you’ll see the image written successfully:
Determine OPT area Due to its file extension (or lack thereof), "factory_defaults.bin" is considered as RAW BINARY format! 11 bytes at 0x4800... OK Bytes written: 11
If the chip is not connected correctly, you’ll see an error; double-check the pin header connections and the solder joints on your board:
Determine OPT area Due to its file extension (or lack thereof), "factory_defaults.bin" is considered as RAW BINARY format! 11 bytes at 0x4800... Tries exceeded
This is not a tutorial on how to program STM8 microcontrollers; here is a better one than I could write, and here is an example project which you can upload in the same way as the
factory_defaults.bin file to test the board’s functions. It reads the resistive input’s analog value, prints it to the 3-digit display, and toggles the relay on / off when the ‘Set’ button is pressed. Note that the relay will not trigger if an external 12V power supply is not connected, but the LED on the board should still light up. If everything works, let’s move on to adding a few last missing components.
Step 1c: Add a Pull-Down Resistor to the Relay-Driving Transitor
Unless I’m mistaken, the missing
R1 resistor is a pull-down for the gate of the NPN transistor which controls the relay. On a less poorly-cloned version that I received from another listing there was a 10K resistor here, and it does sound like a good idea to avoid leaving the gate floating in order to prevent the relay from spuriously triggering. So, let’s add an 0805-sized 10K resistor there:
Step 1d: Add More Missing Capacitors
C1 spot is for a filtering capacitor to reduce analog noise on the input. Typically it is filled with a 470nF capacitor but this resource suggests that that may not be the best design. It sounds like a 4.7nF – 22nF capacitor is a better choice, but it also sounds like you can leave this empty if you want, since it sounds like the capacitor would be so far away from the microcontroller that it is unlikely to be very effective at filtering. Still, I added a small ceramic capacitor.
Several of these boards also arrived with the power supply’s capacitors apparently broken off. Oh well, there are plenty of 1206 ceramic capacitors in the world. I used 10uF:
Step 2: Apply 12V Power
The W1209 has four screw terminals for the higher-power connections. They are clearly marked; The
K1 pins are normally disconnected, and connected by the relay when it is activated by the microcontroller. The
GND terminals are self-explanatory power inputs. The 12V is regulated down to 5V for the microcontroller and LEDs, while the 12V power drives the relay.
But a word of warning, I sort of expect the relay to be of a similar quality to the other components on the board, so I wouldn’t recommend using it for anywhere near the 20A that it claims to be rated for, or allow it to operate unattended. I plan on using these for controlling recycling machines for plastics and easier metals like aluminum, where there will always be a person and an ‘off’ switch nearby when they are in use. If you depend on these relays to deliver a lot of power while nobody is around, then you’re on your own buddy.
Anyways, it’s best to use stranded wires for screw terminals, but you can use breadboard jumper wires in a pinch. Hook everything up, and the relay should click happily when it is turned on or off by the MCU. You can connect the 12V supply’s +/- wires to the
GND screw terminals, and the relay’s load should connect across the
That’s all there is to it; the W1209 can be a cheap and cheerful little tool and learning platform. If you plan on using these for higher temperatures than about 100C like I do, try using a 100K thermistor instead of the 10K one which the boards usually ship with; 3D printer replacement parts work well. Another idea is to use a photodiode instead of a thermistor to perform actions based on the light level. You can use any sort of resistive input, it doesn’t have to be a thermometer.
I would like to follow up with a post that uses this board to learn about 7-segment LED displays, using relays, and listening to resistive sensors. But for now, hopefully the firmware on Github is fairly self-explanatory with the help of Lujji’s excellent introduction to the STM8.