September 20, 2023

Week 3:

Microcontrollers and MicroPython (or Arduino). Homework was to create an interactable program to run on a small board.

This week was pretty light. Our assignment was simply to familiarize ourselves with the basics of microcontroller programming, and create a program that interacted and communicated with the user.

There were a few options for the devices we could use; I ended up going with the Xiao RP2040, a microcontroller by Raspberry Pi. I’ve used Rasperry Pi devices in the past and knew they were quality, so I figured the RP2040 would be my best bet for getting started with things. The device also had a few LEDs, which I thought might come in handy for whatever program I ended up making (spoiler alert: I was right). In terms of software, I opted to use the Arduino IDE, since I had some past experience with it from my own personal projects.

I began by attempting to familiarize myself with the RP2040 by reading its data sheet. Specifically, I needed to determine how to interface with the LEDs on the board, since I wanted to use them for this week’s assignment. Unfortunately, the I/O diagrams on the data sheet didn’t really help with that, so I had to do some digging online.

With some help from my TA Anthony (shoutout) I found this resource which contained a useful diagram detailing which pins I needed to write to in order to control the board’s LEDs. However, when I first attempted to use digitalWrite() to power on an LED, I found that nothing happened. As it turned out, I first had to set the corresponding pin to output mode using pinMode(), otherwise the pin wouldn’t receive my input, and the LED wouldn’t turn on. Once I took care of that, I was able to control the small LEDs in the corner of the RP2040. However, I wanted to use the big Neopixel LED on the front, since it was much bigger and brighter than the corner LEDs (and of course, everyone knows bigger is better).

When I tried to write to the corresponding pin specified on the diagram I was using, I ran into the same issue of nothing happening again. As it turned out, the pin to control the Neopixel LED was incorrectly labelled! Luckily, I found another website on getting started with the RP2040 that included some code to control Neopixel LED. Once I switched to the pin number listed on that website, the LED lit up, and I was in business.

The rest was pretty straightforward. My idea was to create a morse code app, where you could send a string to the board over the Arduino serial connection, and the LED would blink back the message in morse. The code was simple, really just a Serial.read() and an array to translate characters into their morse code equivalents. The one thing I didn’t add was error handling for non-letter characters, but I figured it was unnecessary–we can just say that not entering non-letters is part of the function spec (I’m sure my 6.031 professors would be proud).

Here’s the code, in case you’re interested:


#include <Adafruit_NeoPixel.h>

    int LED_PWR = 11;
    int LED_PIN = 12;
    
    Adafruit_NeoPixel pixels(1, LED_PIN, NEO_GRB + NEO_KHZ800);
    
    void setup() {
        // put your setup code here, to run once:
        pixels.begin();
        pinMode(LED_PWR, OUTPUT);
        digitalWrite(LED_PWR, HIGH);
    }
    
    String morse[26] = {
        ".-", "-...", "-.-.", "-..", ".",
        "..-.", "--.", "....", "..", ".---",
        "-.-", ".-..", "--", "-.", "---",
        ".--.", "--.-", ".-.", "...", "-",
        "..-", "...-", ".--", "-..-", "-.--",
        "--.."
    };
    
    String w = "sos";
    int short_pause = 400;
    int long_pause = 1000;
    void loop() {
        if (Serial.available() > 0) {
        w = Serial.readString();
        Serial.print("New Message Saved: ");
        Serial.println(w);
        pixels.setPixelColor(0, pixels.Color(0, 255, 0));
        pixels.show();
        delay(500);
        pixels.setPixelColor(0, pixels.Color(0, 0, 0));
        pixels.show();
        delay(long_pause);
        }
    
        for(int i = 0; i < w.length(); ++i) {
        char c = tolower(w[i]);
        String w_morse = morse[c - 'a'];
        for(int j = 0; j < w_morse.length(); ++j) {
            pixels.setPixelColor(0, pixels.Color(0, 0, 255));
            pixels.show();
            if (w_morse[j] == '.') {
            delay(short_pause);
            }     
            else {
            delay(long_pause);
            }
            pixels.setPixelColor(0, pixels.Color(0, 0, 0));
            pixels.show();
            delay(short_pause);
        }
        pixels.setPixelColor(0, pixels.Color(0, 0, 0));
        pixels.show();
        delay(long_pause);
        }
        pixels.setPixelColor(0, pixels.Color(255, 0, 0));  // end of word
        pixels.show();
        delay(1000);
        pixels.setPixelColor(0, pixels.Color(0, 0, 0));
        pixels.show();
        delay(short_pause);
    }