Week 9: Output Devices

March 25, 2024

Output Devices

The objective of this project was twofold: to gain hands-on experience with various output devices and to test the PCB designs embedded within the ToGO cubes as part of my final project. I explored the use of RGB LEDs, servo motors, and displays as output devices, integrating them into my designs to understand their functionality and application better.

Alt Text

Group Assigment Page

group assignment link 🚀

Welcoming KiCad into Output Design

For the ToGoV1 project, I planned the integration of 2 WS2812B RGB LEDs, a 1X04 pin socket for the display, and a 1X06 pin socket output from which I intended to experiment with a servo motor. I chose the Xiao ESP32C3 as the microcontroller “brain” of my project. In my KiCad library search, I couldn’t find a specific component for the display. To address this, I opted to add a 4-pin socket to serve as the display connection. My experience was based on using KiCad version 7.0.6 on macOS Sonoma 14.4.1.

Alt Text Alt Text Alt Text

Used Outputs Alt Text

The brain of my project

Alt Text Alt Text

PCB creation time Alt Text Alt Text Alt Text

Output 1: Display

The display required special attention as it was an OEM model. I encountered issues when attempting to use different libraries; none seemed to work. Eventually, I found a recommendation on the Xiao ESP32C3 wiki page suggesting the use of the U8g2 library under the I2C hardware connection guide. For the PCB connections, I connected the SCL (Serial Clock Line), SDA (Serial Data Line), 5V, and GND accordingly. A key learning point here was understanding the significance of SCL and SDA for I2C protocol. Particularly in projects with limited pin availability, using these lines for I2C communication proved to be essential.

I utilized Arduino IDE version 2.2.2. For the display, I added the U8g2 library (version 2.34.22).

Alt Text

//#include <Arduino.h>
#include <U8g2lib.h>
 
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);    //Low spped I2C
 
void setup(void) {
  u8g2.begin();
//  u8x8.setFlipMode(1);   // set number from 1 to 3, the screen word will rotary 180
}
 
void loop(void) {
  u8g2.clearBuffer();                   // clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tr);   // choose a suitable font
  u8g2.drawStr(0,15,"Fab Academy");    // write something to the internal memory
  u8g2.drawStr(0,30,"Aalto!");
  u8g2.drawStr(0,40,"Sedat Yalcin!");
  u8g2.sendBuffer();                    // transfer internal memory to the display
//  delay(1000);  
} 

Output 2: Servo I conducted an experiment using a servo motor connected to the VCC (5V), GND, and pin 11. Through this process, I learned the importance of carefully selecting specific pins for use, especially with the ESP32-C3 microcontroller. It’s advisable to consult the pinout diagram of the microcontroller to ensure the correct design and functionality. Selecting the appropriate pins based on the microcontroller’s capabilities and the requirements of the servo motor is crucial for the success of any project involving servo control.

Alt Text

#include <Servo.h>

Servo myservo;  // Create a Servo object

int servoPin = 11; // Pin to which the servo motor is connected

void setup() {
  myservo.attach(servoPin); // Attach the servo motor to the pin
}

void loop() {
  for (int angle = 0; angle <= 180; angle += 1) { // Rotate the servo motor from 0 to 180 degrees
    myservo.write(angle);             
    delay(15); // Wait for 15 milliseconds                       
  }
  for (int angle = 180; angle >= 0; angle -= 1) { // Rotate the servo motor back from 180 to 0 degrees
    myservo.write(angle);             
    delay(15); // Wait for 15 milliseconds                       
  }
}

Code Turn Left/Rigth

#include <U8g2lib.h>
#include <ESP32Servo.h>
#include <Adafruit_NeoPixel.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define NEOPIXEL_PIN 3 // Pin where the NeoPixel LED is connected
#define NUMPIXELS    1 // Number of NeoPixel LEDs

Adafruit_NeoPixel pixels(NUMPIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);

Servo myservo;  // Create a Servo object
int servoPin = 10; // Pin where the servo motor is connected

// OLED display setup
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); //Low speed I2C

void setup() {
  u8g2.begin();
  myservo.attach(servoPin); // Attach the servo motor to the pin

  pixels.begin(); // Initialize the NeoPixel
}

void loop() {
  myservo.write(90); // Set the servo motor to 90 degrees
  displayMessage("Right");
  setPixelColor(255, 0, 0); // Red
  delay(1000);

  myservo.write(0); // Set the servo motor to 0 degrees
  displayMessage("Left");
  setPixelColor(0, 255, 0); // Green
  delay(1000);
}

void displayMessage(const char* message) {
  u8g2.clearBuffer();                  
  u8g2.setFont(u8g2_font_ncenB08_tr);   
  u8g2.drawStr(0,15,message);   
  u8g2.sendBuffer();                    
}

void setPixelColor(uint8_t r, uint8_t g, uint8_t b) {
  pixels.setPixelColor(0, pixels.Color(r,g,b)); 
  pixels.show(); 
}

It’s works

Source Files

Source Folder