Output devices


Learning outcomes

Demonstrate workflows used in controlling an output device(s) with MCU board you have designed.

Group assignment

The group assignment for this week here is the summary of what we have done:

  • Measure power consumption of output devices.
  • Document findings on group page.
  • Reflect on individual page.

DC Power Supply: Instek GPD-3303D

  • Converts AC to DC.
  • Provides stable output voltage.
  • Maximum voltage: 30V
  • Maximum current: 3A per channel, 5A total
  • Accuracy: ±(0.1% + 2 digits)

Output Device 1: DC Motor

  • Converts electrical to mechanical energy.
  • Voltage range: 6-12V.
  • Powered at 12V, drew 0.04A.
  • Power consumption: 0.48W
  • Stalled at 12V, drew 1.29A.

Output Device 2: White LED

  • Emits white light with forward current.
  • Voltage range: 3-3.4V.
  • Powered at 3.2V, drew 0.02A.
  • Power consumption: 0.064W

Individual assignment

This week's focus is on output devices. While I previously designed a board for controlling leds by push bottons and I have already some pins to add servo motor with considering the common ground for Week 8, I'll be reusing the same board this time to control a servo motor instead.

I will be utilizing an SG90 servo motor for this project. The SG90 servo motor features three lines: two for power supply and the third for data transmission, allowing control over the motor's position of rotation through PWM (Pulse Width Modulation) signals.

A servo motor, like the SG90, is a type of motor commonly used in robotics and other applications where precise control of angular position is required. Unlike typical DC motors, servo motors can rotate to specific angles based on the duration of an input pulse.

Pulse Width Modulation (PWM) is the technique used to control the position of a servo motor. PWM works by varying the width of pulses in a periodic signal, where the width of the pulse corresponds to a specific position of the servo motor's shaft.

In the case of the SG90 servo motor, the data line is connected to a microcontroller's PWM-capable GPIO pin. By sending PWM signals with varying pulse widths to the servo motor, we can control its position. Typically, a pulse width of around 1ms corresponds to one extreme position (e.g., 0 degrees), while a pulse width of around 2ms corresponds to the opposite extreme position (e.g., 180 degrees). Intermediate positions can be achieved by adjusting the pulse width accordingly.

In summary, the relationship between the servo motor and PWM lies in the fact that PWM signals are used to control the position of the servo motor's shaft, allowing for precise angular positioning in various applications.

Below is a sample code written in Arduino IDE for controlling a servo motor connected to pin 26 and an LED connected to pin 1. The servo motor will move to a specific angle when a push button connected to pin 29 is pressed:

#include <Servo.h>

                                        #define SERVO_PIN 26  // Define the pin for the servo motor
                                        #define LED_PIN 1     // Define the pin for the LED
                                        #define BUTTON_PIN 29 // Define the pin for the push button
                                        
                                        Servo servo; // Create a servo object
                                        
                                        void setup() {
                                          servo.attach(SERVO_PIN); // Attach the servo to its pin
                                          pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
                                          pinMode(BUTTON_PIN, INPUT_PULLUP); // Set the button pin as an input with internal pull-up resistor
                                        }
                                        
                                        void loop() {
                                          if (digitalRead(BUTTON_PIN) == LOW) { // Check if the button is pressed
                                            // Turn on the LED
                                            digitalWrite(LED_PIN, HIGH);
                                        
                                            // Move the servo to a specific angle (e.g., 90 degrees)
                                            servo.write(90);
                                        
                                            // Add a delay to ensure stable servo movement
                                            delay(500);
                                          } else {
                                            // Turn off the LED
                                            digitalWrite(LED_PIN, LOW);
                                        
                                            // Move the servo to another angle (e.g., 0 degrees)
                                            servo.write(0);
                                        
                                            // Add a delay to ensure stable servo movement
                                            delay(500);
                                          }
                                        }
                                        

For some fun I tried to add oled 0.96 inch to the xiao rp2040. I used this link to help me to utilize the circuit

The 0.96-inch OLED (Organic Light Emitting Diode) display is a small, lightweight, and energy-efficient display module that offers high brightness, contrast, and visibility compared to traditional LCD displays. OLED technology utilizes organic compounds that emit light when an electric current is applied, allowing for self-emitting pixels that eliminate the need for a backlight.

Here is the conection for the xiao rp2040 anthe oled 0.96 inch

To add a 0.96-inch OLED display to a XIAO RP2040, you'll need to follow these steps:

  1. Gather Components:

    • XIAO RP2040 board
    • 0.96-inch OLED display module
    • Jumper wires (if needed)
    • Breadboard (if needed)
  2. Connections:

    • Identify the pins on both the XIAO RP2040 and the OLED display module.
    • Connect the corresponding pins between the XIAO RP2040 and the OLED display. Common connections include:
      • SDA (Serial Data) to a GPIO pin on the XIAO RP2040 (e.g., pin 2)
      • SCL (Serial Clock) to another GPIO pin on the XIAO RP2040 (e.g., pin 3)
      • VCC (Power) to the 3.3V pin on the XIAO RP2040
      • GND (Ground) to any GND pin on the XIAO RP2040
  3. Install Libraries:

    • Open the Arduino IDE.
    • Go to Sketch > Include Library > Manage Libraries.
    • In the Library Manager, search for "OLED" or "SSD1306".
    • Install the library that supports the SSD1306 OLED display module.
  4. Write Code:

    • Start a new Arduino sketch.
    • Include the necessary libraries at the beginning of your code. For example:
      cpp
      #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
    • Initialize the OLED display object in your setup function:
      cpp
      #define OLED_RESET -1 Adafruit_SSD1306 display(OLED_RESET);
    • In the setup function, initialize the OLED display and any other setup tasks:
      cpp
      void setup() { // Initialize the OLED display with its I2C address (0x3C) if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } // Clear the buffer display.clearDisplay(); // Add setup tasks here }
    • In the loop function, update the content on the OLED display:
      cpp
      void loop() { // Clear the previous content display.clearDisplay(); // Add content to display (text, shapes, etc.) display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0,0); display.println("Hello, world!"); // Update the display display.display(); // Add loop tasks here }
  5. Upload Code:

    • Connect the XIAO RP2040 to your computer via USB.
    • Select the correct board and port from the Arduino IDE.
    • Click the upload button to compile and upload the code to the XIAO RP2040.
  6. Test:

    • After uploading the code, verify that the OLED display is functioning properly. You should see the specified content displayed on the OLED screen.