As an Amazon Associate we earn from qualifying purchases.
Blinking an LED is the first program most people run on a new microcontroller. It proves that your toolchain works, that the chip is powered correctly, and that you can upload code. This guide shows how to blink an LED on the STM8S003K3 from the Arduino environment, explains what each line of the sketch does, and covers the mistakes that most often stop it from working.
What is the STM8S003K3?
The STM8S003K3 is a low-cost 8-bit microcontroller from STMicroelectronics, part of the STM8S “value line”. It has 8 KB of flash memory, 1 KB of RAM and runs at up to 16 MHz. The “K” in the name refers to its 32-pin package, so it offers more I/O pins than the smaller 20-pin STM8S003F3.
The STM8 family is not supported by the official Arduino boards manager. To use it with the Arduino IDE you need a community STM8 core, such as sduino, which adds STM8 boards and uses the SDCC compiler. Check the board list of whichever core you install to confirm your exact chip or a compatible variant is covered. Pin names and available functions can differ between cores.
What you need
- An STM8S003K3 chip on a breakout or development board
- An ST-LINK/V2 programmer (or compatible clone) that supports the SWIM interface
- The Arduino IDE with an STM8 core installed
- One LED and a current-limiting resistor (a few hundred ohms is typical)
- Jumper wires and a breadboard
Wiring the LED
The sketch below uses pin PD7, which is port D, bit 7. Connect it like this:
- Connect PD7 to one end of the resistor.
- Connect the other end of the resistor to the LED’s longer leg (the anode).
- Connect the LED’s shorter leg (the cathode) to GND.
- Connect the SWIM, reset, power and ground lines from the ST-LINK to the matching pins on your board.
Always use a resistor. Without one, the LED can draw more current than the pin should supply, and either part can be damaged.
The original blink code
This is the code the article started with. It mixes the Arduino functions (pinMode, delay) with calls from ST’s Standard Peripheral Library (GPIO_Init, GPIO_WriteReverse).
#define LED_GPIO_PORT (GPIOD)
#define LED_GPIO_PINS (GPIO_PIN_7 | GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0)
#define led PD7
void setup() {
GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
pinMode(led, OUTPUT);
}
// the loop function runs over and over again forever
void loop()
{
GPIO_WriteReverse(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS);
delay(1000);
// wait for a second
}How the code works
The defines
LED_GPIO_PORT selects port D. LED_GPIO_PINS combines pins 7, 2, 1 and 0 of that port into a single mask using the bitwise OR operator. led is the Arduino-style name for PD7.
setup()
GPIO_Init configures every pin in the mask as an output. The mode GPIO_MODE_OUT_PP_LOW_FAST means push-pull output, starting low, with fast switching. The pinMode(led, OUTPUT) line then sets PD7 as an output again through the Arduino API. For PD7 this is redundant, but it does no harm.
loop()
GPIO_WriteReverse flips the state of every pin in the mask: high becomes low and low becomes high. delay(1000) pauses for 1000 milliseconds, so the LED changes state once per second and completes one full blink every two seconds.
A note on the pin mask
Because the mask includes PD7, PD2, PD1 and PD0, the code toggles all four pins together. If you only connected an LED to PD7, that works, but anything else wired to PD0, PD1 or PD2 will also toggle. That can be confusing if those pins are connected to other parts of your board. If you only need one LED, keep the mask to a single pin.
A simpler Arduino-only version
If your core supports the standard Arduino functions for this chip, you can skip the Standard Peripheral Library entirely. This version is easier to read and easier to move to other boards:
#define LED PD7
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH); // LED on
delay(1000); // wait one second
digitalWrite(LED, LOW); // LED off
delay(1000); // wait one second
}This turns the LED on for one second and off for one second. If you want a faster blink, reduce the delay values. If you want a different pin, change the define to match the pin name your core uses.
Uploading the sketch
- Install an STM8 core in the Arduino IDE through the boards manager or by following that core’s instructions.
- Select the STM8S003 board or the closest match from the Tools menu.
- Choose the ST-LINK as the upload method.
- Connect the ST-LINK to the chip’s SWIM pin, reset pin, ground and power.
- Click Upload and wait for the process to finish.
Troubleshooting
- The LED never lights: check the LED polarity first. Then confirm the resistor is in series and that the pin name in the sketch matches the physical pin you used.
- Upload fails: check the SWIM, reset and ground connections, and make sure the chip is powered. A programmer clone may also need a driver.
- Compile errors about GPIO functions: the Standard Peripheral Library headers may not be available in your core. Use the Arduino-only version above.
- The LED stays dim or always on: you may be toggling several pins at once or have the LED connected the wrong way around. Check the wiring against the pin mask.
- Wrong timing: the delay depends on the chip’s clock setting. If the blink rate looks off, check the clock option selected in the board menu.
Where to go next
Once the LED blinks, try changing the delay, adding a second LED, or reading a button. You can also compare this with the classic Arduino LED blink on a standard board to see how little the sketch changes. If you work with other chips, our guides to blinking an LED on an 8051 and the dsPIC30F2010 blink example show the same idea with different tools. When you are ready to stop relying on blocking delays, look at timer-based LED blinking.
FAQ
Can I program the STM8S003K3 with the Arduino IDE?
Yes, with a community core such as sduino installed. The official Arduino boards manager does not include STM8 chips, so you must add the core yourself.
What programmer do I need?
An ST-LINK/V2 or compatible clone that supports the SWIM interface is the usual choice. It connects to the chip’s SWIM and reset pins.
Why does the original code use both GPIO_Init and pinMode?
GPIO_Init comes from ST’s Standard Peripheral Library and sets up a group of pins at once. pinMode is the Arduino way of doing the same for one pin. For PD7 the second call is redundant, so you can remove either one.
How do I make the LED blink faster?
Reduce the number in delay(). For example, delay(250) toggles the LED four times per second.