Saturday, April 11, 2020

0000 0000 0010 1010

STM8 super cheap breakout board

Late one night ( ¯\_(ツ)_/¯ ) I spotted a STM8S103F3P6 breakout board on Aliexpress that seemed too cheap to be real (~AU90 cents). So of course I ordered 5 of them. When they arrived I plugged them into my computer using a micro-usb adapter and ... nothing. So the price is now starting to make sense. The micro-usb connector is only for power, not data.
Little board, lotsa power

Still, there must be a way to talk to this chip, and at least get some quality blinky action going on. I happen to have an ST-Link programmer left over from an exploration of the famous STM32 "Blue Pill" from a year or so ago. After a bit of searching online, I was directed to SDuino which promised "within a few minutes you are ready to compile and upload your first STM8S-based project" which sounded great!


After downloading and installing the STM8 board manager and specifying the ST-LINK programmer, I was able to successfully blink and fade an LED and all was right with the world.

Success?

Or was it? Really this thing would only be useful if I did some interesting project - not the standard blinky. So I thought maybe I might attach a button to one of the pins, and then have the board turn on an RGB LED in a sort of lazy traffic light indicator when the button is pushed. Push the button, stop the traffic, sprint across the road.

Way too much head-scratching later (documentation and examples are a bit thin on the ground for this device), and we have the following code...



#define RLED PC3          // -|
#define GLED PC4          //  | PWM pins
#define BLED PA3          //  |
#define BUTTON PA2        // button pin

// two checks of button press for button bounce
boolean buttonpress1 = false;
boolean buttonpress2 = false;

void setup() {            // set up output and input pins
    pinMode(RLED, OUTPUT);
    pinMode(GLED, OUTPUT);
    pinMode(BLED, OUTPUT);
    pinMode(BUTTON, INPUT_PULLUP);
}

void blanklights() {      // kill all pwm between transitions
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, LOW);
    digitalWrite(BLED, LOW);
}

void lightcycle() {       // change values to change light show

    // dim red
    for (byte mystep = 250; mystep > 0; mystep--) {
        analogWrite(RLED, mystep);
        delay(2);
    }
    digitalWrite(RLED, LOW);

    blanklights();
    delay(200);

    // bring up "yellow" (?)
    for (byte mystep = 0; mystep < 150; mystep++) {
        analogWrite(RLED, mystep / 1.5);
        analogWrite(GLED, mystep / 1.2);
        analogWrite(BLED, mystep / 1.2);
        delay(5);
    }

    delay(2000); // hold yellow

    // lower yellow
    for (byte mystep = 150; mystep > 0; mystep--) {
        analogWrite(RLED, mystep / 1.5);
        analogWrite(GLED, mystep / 1.2);
        analogWrite(BLED, mystep / 1.2);
        delay(5);
    }

    blanklights();
    delay(200);

    // bring up green
    for (byte mystep = 0; mystep < 250; mystep++) {
        analogWrite(GLED, mystep);
        delay(10);
    }

    delay(5000); // hold green 5 seconds (run!)

    // reduce green
for (byte mystep = 250; mystep > 0; mystep--) { analogWrite(GLED, mystep); delay(5); } digitalWrite(GLED, LOW); blanklights(); delay(200); // bring up red for (byte mystep = 0; mystep < 250 ; mystep++) { analogWrite(RLED, mystep); delay(2); } digitalWrite(GLED, LOW); digitalWrite(RLED, HIGH); } void loop() { buttonpress1 = digitalRead(BUTTON); delay(10); buttonpress2 = digitalRead(BUTTON); if ((buttonpress1) && (buttonpress2)) { lightcycle(); } else { digitalWrite(RLED, HIGH); delay(100); } }

Originally I was going to put the little one to sleep and wake it up for traffic light action, but alas I could not find any decent manuals, example code, forums, etc. where I could see how to do this, so I settled on the looped checking for button code you see above.

It's not great, and I'm not really happy to the have the chip buzzing away checking a digital read constantly (now that will drain a battery), so I will continue the quest for putting this little guy to sleep to save power - but for now it works fine.



I like traffic lights, especially when they're green





1 comment: