logo

Smart Livestock Management

Home Final Project About Me

Week 04 Documentation

Embedded Programming


Goals for Week 04:

  • Do the group assignment,
  • Browse through the datasheet for your microcontroller,
  • Write a program for a microcontroller, and simulate its operation, to interact (with local input &/or output devices) and communicate (with remote wired or wireless connection).
  • Group assignment page & design file:

    Wowki

    1st attempt: Pico & Arduino Language with OLED Display

    My original idea was to use the Pico, an OLED display, and a keypad to make an input from the keypad show up on the OLED display. However, I would later experience some difficulty with the keypad, but the OLED display worked fine. I would later switch to an Arduino Uno partially because there's more documentation. But regardless of whether we're using an uno or Pico, we'll need the same libraries, which are Adafruit SSD1306 & Adafruit GFX. I got the information on which libraries to use at this source.

    cutter img cutter img

    This is the wiring for the OLED display, which I learned how to do from this source. This is the program while running, I've given it input to display "hello world".

    cutter img

    This is my code for the OLED display, which does work:

    .ino code

              
                #include 
                  #include 
                  #include 
                  
                  
                  #define SCREEN_WIDTH 128 // OLED display width, in pixels
                  #define SCREEN_HEIGHT 64 // OLED display height, in pixels
                  
                  // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
                  Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
                  
                  void setup() {
                    Serial.begin(115200);
                  
                    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
                      Serial.println(F("SSD1306 allocation failed"));
                      for(;;);
                    }
                    //delay(2000);
                    display.clearDisplay();
                    display.setTextSize(1);
                    display.setTextColor(WHITE);
                    display.setCursor(0, 10);
                    // Display static text
                    
                    display.println("Hello, world!!!");
                    display.display(); 
                  }
                  
                  void loop() {
                    Serial.println("slay");
                  }
                  
              
            

    .json code

              
                {
                  "version": 1,
                  "author": "Hanna Ondrasek",
                  "editor": "wokwi",
                  "parts": [
                    { "type": "wokwi-pi-pico", "id": "pico", "top": 0, "left": 0, "attrs": {} },
                    {
                      "type": "board-ssd1306",
                      "id": "oled1",
                      "top": 3.14,
                      "left": -143.77,
                      "attrs": { "i2cAddress": "0x3c" }
                    },
                    {
                      "type": "wokwi-membrane-keypad",
                      "id": "keypad1",
                      "top": -126.8,
                      "left": 101.6,
                      "attrs": {}
                    }
                  ],
                  "connections": [
                    [ "pico:GP0", "$serialMonitor:RX", "", [] ],
                    [ "pico:GP1", "$serialMonitor:TX", "", [] ],
                    [ "oled1:GND", "pico:GND.8", "black", [ "v-38.4", "h192", "v57.6" ] ],
                    [ "oled1:VCC", "pico:3V3", "red", [ "v-19.2", "h192.15", "v57.6" ] ],
                    [ "oled1:SDA", "pico:GP4", "yellow", [ "v0" ] ],
                    [ "oled1:SCL", "pico:GP5", "orange", [ "v0" ] ]
                  ],
                  "dependencies": {}
                }
              
            

    However, I started the encounter some issues when I tried to have the keypad simply print to the serial at first. The program would not even enter the setup or loop, as the debug message would never be printed in serial. There wasn't a lot of documentation on pico in .ino issues with keypad, so I decided to move onto the uno, which has more documentation.

    cutter img

    Here are the libaries I used to make my OLED and keypad configuration work:

    cutter img

    The wiring for the final design, which I learned from this source for the keypad and this source for the OLED display.

    cutter img

    My finished code. The code does have 1 issue, where since the keypad input is non-static, there must be a delay because the keypad input will immediately disappear from the OLED display. I tried Serial.read() & Serial.readString(), but they did not work for me. I think there may be something extra I have to do to read things from serial in wowki.

    .ino

                  
                    #include 
                      #include 
                      #include 
                      #include 
                      
                      const int SCREEN_WIDTH = 128; // OLED display width, in pixels
                      const int SCREEN_HEIGHT = 64; // OLED display height, in pixels
                      
                      
                      const uint8_t ROWS = 4;
                      const uint8_t COLS = 4;
                      char keys[ROWS][COLS] = {
                        { '1', '2', '3', 'A' },
                        { '4', '5', '6', 'B' },
                        { '7', '8', '9', 'C' },
                        { '*', '0', '#', 'D' }
                      };
                      
                      uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins
                      uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins
                      
                      Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
                      // Declaration for an SSD1306 display 
                      Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
                      
                      
                      void setup() {
                        Serial.begin(9600);
                        if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
                          Serial.println(F("SSD1306 allocation failed"));
                          while(true);
                        }
                      }
                      
                      void loop() {
                        char key = keypad.getKey();
                      
                      
                        if (key != NO_KEY) {
                          Serial.println(key);
                        }
                      
                        display.clearDisplay();
                      
                        display.setTextSize(1);
                        display.setTextColor(WHITE);
                        display.setCursor(0, 10);
                        // Display static text
                        display.println(key);
                        display.display();
                        // text from oled display will immediately disappear w/o delay
                        // already tried Serial.readString() & Serial.read() 
                        delay(3000);
                      }
                  
                

    .json

                  
                    {
                      "version": 1,
                      "author": "Hanna Ondrasek",
                      "editor": "wokwi",
                      "parts": [
                        { "type": "wokwi-arduino-uno", "id": "uno", "top": 402.94, "left": 25, "attrs": {} },
                        { "type": "wokwi-membrane-keypad", "id": "keypad", "top": -2, "left": 92, "attrs": {} },
                        {
                          "type": "board-ssd1306",
                          "id": "oled1",
                          "top": 655.94,
                          "left": 134.63,
                          "attrs": { "i2cAddress": "0x3c" }
                        }
                      ],
                      "connections": [
                        [ "uno:GND.1", "lcd:VSS", "black", [ "v-51", "*", "h0", "v18" ] ],
                        [ "uno:GND.1", "lcd:K", "black", [ "v-51", "*", "h0", "v18" ] ],
                        [ "uno:GND.1", "lcd:RW", "black", [ "v-51", "*", "h0", "v18" ] ],
                        [ "uno:2", "keypad:C4", "brown", [] ],
                        [ "uno:3", "keypad:C3", "gray", [] ],
                        [ "uno:4", "keypad:C2", "orange", [] ],
                        [ "uno:5", "keypad:C1", "pink", [] ],
                        [ "uno:6", "keypad:R4", "blue", [] ],
                        [ "uno:7", "keypad:R3", "green", [] ],
                        [ "uno:8", "keypad:R2", "purple", [ "v-14", "h0" ] ],
                        [ "uno:9", "keypad:R1", "gold", [ "v-18", "h0" ] ],
                        [ "oled1:GND", "uno:GND.3", "black", [ "v0" ] ],
                        [ "oled1:VCC", "uno:5V", "red", [ "v0" ] ],
                        [ "oled1:SDA", "uno:A4", "green", [ "v-38.4", "h67.27" ] ],
                        [ "oled1:SCL", "uno:A5", "green", [ "v-38.4", "h86.7" ] ]
                      ],
                      "dependencies": {}
                    }
                  
                

    The finished program::