STM8S003K3 BLINKING IN ARDUINO

Photo of author

By Jackson Taylor

Home » Programming » STM8S003K3 BLINKING IN ARDUINO

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.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

What you need

Wiring the LED

The sketch below uses pin PD7, which is port D, bit 7. Connect it like this:

  1. Connect PD7 to one end of the resistor.
  2. Connect the other end of the resistor to the LED’s longer leg (the anode).
  3. Connect the LED’s shorter leg (the cathode) to GND.
  4. Connect the SWIM, reset, power and ground lines from the ST-LINK to the matching pins on your board.
See also
17V-24V DC TO 48V DC BOOST CONVERTER WITH CLOSED LOOP CONTROL USING ARDUINO AND IGBT

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.

See also
IEEE ELECTRONICS / ELECTRICAL

Uploading the sketch

  1. Install an STM8 core in the Arduino IDE through the boards manager or by following that core’s instructions.
  2. Select the STM8S003 board or the closest match from the Tools menu.
  3. Choose the ST-LINK as the upload method.
  4. Connect the ST-LINK to the chip’s SWIM pin, reset pin, ground and power.
  5. 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.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

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.

See also
PUSH PULL SINE PWM BASED INVERTER USING ARDUINO

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.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works