Computing stuff tied to the physical world

A few simple sketches for ARM

Let’s look at some code, to see what the IDE w/ Arduino-STM add-on is capable of.

First, the fabulously famous “Hello world” of Physical Computing, i.e. blinking an LED:

    const int LED = PA1; // inverted logic

    void setup () {
        pinMode(LED, OUTPUT);
    }

    void loop () {
        digitalWrite(LED, LOW);     // on!
        delay(500);
        digitalWrite(LED, HIGH);    // off!
        delay(500);
    }

This is virtually identical to the code on Arduino hardware. The only difference is that pin numbers are more easily identified by the names (“PA1”) normally used with ARM µCs.

Here’s how to send a periodic message to the serial port (that’s the USB port in this case!):

    void setup () {
        Serial.begin(115200);
    }

    void loop () {
        Serial.println(millis());
        delay(1000);
    }

Again, no surprises. Want to turn an LED on with a button? No sweat:

    const int LED = PA1;    // inverted logic
    const int BUTTON = PB0; // inverted logic

    void setup () {
        pinMode(LED, OUTPUT);
        pinMode(BUTTON, INPUT_PULLUP);
    }

    void loop () {
        digitalWrite(LED, digitalRead(BUTTON));
    }

Making the LED fade using PWM and the Arduino’s analogWrite() function:

    const int LED = PA1; // inverted logic

    void setup () {
        pinMode(LED, OUTPUT);
    }

    void loop () {
        // 255 is fully off, 0 is maximally on
        for (int i = 255; i >= 0; --i) {
            analogWrite(LED, i);
            delay(5);
        }
        for (int i = 0; i <= 255; ++i) {
            analogWrite(LED, i);
            delay(5);
        }
    }

Read out an analog value with the 12-bit ADC and report it to the serial port? Peanuts:

    void setup () {
        Serial.begin(115200);
    }

    void loop () {
        Serial.println(analogRead(PA3));
        delay(1000);
    }

As you can see, things work in virtually the same way on STM32 boards as what you have been used to on ATmega’s, such as Arduino and JeeNode boards. Easy sailing!

Access to the built-in I2C and SPI peripherals is equally simple, and so is the connection of an RFM69 wireless radio module. To be described next in a neat little project…

[Back to article index]