Skip to content

10. Output devices

Group assignment:

Measure the power consumption of an output device. Document your work on the group work page and reflect on your individual page what you learned.

Individual assignment:

Add an output device to a microcontroller board you’ve designed and program it to do something. Learning outcomes

Group assignment:

For the Week 10 group assignment on output devices, we worked together to measure the power consumption of a servo motor using a custom PCB and a bench power supply. We programmed an MG90S servo motor with a sweep code in the Arduino IDE, connected it to the PCB, and powered the setup with the supply set at 5V and a maximum current of 0.5A. By observing the current drawn while the servo was sweeping, we found that the servo consumed up to ~0.3A, resulting in a maximum power usage of about 1.5W. This exercise helped me understand how to practically measure and calculate the power consumption of electronic components, which is crucial for designing efficient and reliable circuits. Overall, I learned how to set up the test, interpret the readings, and apply this knowledge to optimize power usage in my own projects

GROUP ASSIGNMENT

Individual Assignment

For this week, I chose to work with an SSD1306 OLED display as my output device. OLEDs are compact, energy-efficient, and provide crisp, high-contrast text and graphics, making them ideal for embedded projects like timers and clocks. I also used a rotary encoder for user input, allowing mode selection and value adjustment.

This PCB is a redesign from my Input Devices Week, where I initially tested the rotary encoder. For Output Week, I added connections and support for the OLED display, improving the board’s versatility for my final project

I tried to use the autorouter to the PCB design . There was some issues . It didnot work so i did the routing manually .

image.png

nothing worked

image.png

This is the Final Circuit Diagram that i routed manualy .

image.png

Then i have Uploaded to Gerber2Png and used Doubleside to Gernerate the Png files Required for milling .

image.png

added capacitors to rotary encoders as my instructor instructed . I added capacitors to the A and B pins of the rotary encoder to help filter out noise and prevent false readings. When you turn the encoder, the mechanical contacts inside can create tiny, fast spikes called "bounces" that might confuse the microcontroller and cause it to count incorrectly. The capacitors smooth out these spikes, so each step or click of the encoder is counted accurately and the readings are much more stable.

image.png

The below image is my Final circuit diagram . .

image.png

initial pcb.jpg

I have milled the PCb then i Realized that the Led is not connected Properly and then i found the connections are wrong and then i used a heat Gun to De solder it . It was a mistake . As the Heat Gun burned my led . I Told my instructor about this and he told there is a different way on how to use with LEDS .

hotairgun.jpg

The below image is the heated led where the top portion of the led has burned Off .

hot airgun.jpg

How to check the Pcb before connecting to your Laptop?

For that you could use the help of a power supply where the input voltage and current are set . for me the Input volatge was 5v and the ampere was set to 0.1 Ampere in channel 1.

Steps > choose the chanel > > Set the current and the voltage >connect the Probe accordingly the Red to the vcc and then black probe to the Ground > click the output .

checking2.JPG

checking connection.jpg

checking1.jpg

If the current delivered is constant and if there is no over current drawn you could say the circuit is not short . If the circuit is short it will try to withdraw over current and you may see something like cc. (since the current is limited in the power supply as we have set it already it wont be a problem ). which means the circuit is short and you should check the connections . if it is short then connected to laptop . it may damage your laptop driver .

rotary encoder.jpg

rotary encoder2.jpg

i tested the normal encoder codes from the last week . it was not working .

image.png

image.png

OLED DISPLAY .

OLED, or Organic Light-Emitting Diode, display is a type of electronic visual display technology that uses organic compounds to emit light when an electric current is applied. Unlike traditional LCD (Liquid Crystal Display) screens, which require a backlight to illuminate pixels, OLED displays are self-emissive. This means that each individual pixel in an OLED display emits its own light, allowing for deep blacks, high contrast ratios, and vibrant colors. OLED technology is known for its advantages, including faster response times, wider viewing angles, and reduced power consumption compared to some other display technologies.

Step Description
1. Install Libraries Install the Adafruit_GFX and Adafruit_SSD1306 libraries via the Arduino Library Manager. These libraries simplify drawing graphics and interfacing with SSD1306-based OLEDs.
2. Hardware Connections Connect the OLED's SDA and SCL pins to GPIO 21 and 22 of the ESP32, respectively. Ensure proper power connections (3.3V and GND).
3. Initialize Display Use display.begin(SSD1306_I2C_ADDRESS, SDA, SCL) in your setup code to initialize the display with its I2C address (commonly 0x3C).
4. Clear and Setup Screen Use display.clearDisplay() to clear the screen before drawing and display.display() to update the content on the OLED.
5. Draw Graphics/Text Utilize functions like display.setTextSize(), display.setTextColor(), and display.setCursor() to add text or shapes. For example, draw a rectangle using display.drawRect(x, y, width, height, color).
6. Display Dynamic Data Update content dynamically by clearing the screen and rewriting data in a loop. For instance, use a timer to fetch real-time data like time from an NTP server.
7. Optimize Power Consumption Turn off the display when idle using display.ssd1306_command(SSD1306_DISPLAYOFF) to save power in battery-powered applications.

I2C

The SSD1306 OLED is connected via I2C, using GPIO21 (SDA) and GPIO22 (SCL) on the ESP32. I2C is a two-wire protocol that allows multiple devices to share the same bus. The OLED’s address is set to 0x3C. In the code, Wire.begin(21, 22); initializes the I2C communication, and the display library handles all further interactions.

oledhello.jpg

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SDA_PIN 21
#define SCL_PIN 22

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

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

// Initialize I2C with specific pins
Wire.begin(SDA_PIN, SCL_PIN);

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}

// Clear the buffer
display.clearDisplay();

// Set text properties
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(25, 25);

// Display "Hello"
display.println("Hello");

// Show on the display
display.display();
}

void loop() {
// Your main code can go here
}

![oledtime.jpg](../images/WEEK10/oledtime.jpg)
To program the OLED, I used the Adafruit SSD1306 library in Arduino. I also experimented with AI-generated code to quickly prototype display functions.

Prompt used:(more in pdf )

“Write Arduino code to display the current time from an NTP server on an SSD1306 OLED using ESP32, with I2C on pins 21 and 22.”

Code Explanation: The code initializes the OLED and connects to WiFi, then fetches the time from an NTP server and displays it. To change the time zone (for example, to display the time in another country), simply adjust the gmtOffset_sec variable in the code to match the desired time zone offset

Displaying Time Using NTP Servers

Step Description
Include Libraries Add WiFi, NTPClient, and Adafruit_SSD1306 libraries to your project
Initialize WiFi Connect to a WiFi network using your SSID and password
Configure NTP Client Set up NTPClient with a pool server (e.g., "pool.ntp.org")
Fetch Time Use NTPClient's update() and getFormattedTime() functions
Display on OLED Write the fetched time to the OLED display using Adafruit_SSD1306 functions
Periodic Updates Implement a timer to update the displayed time every minute
#include <WiFi.h>
#include <time.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Network credentials
const char* ssid = "iPhone";
const char* password = "abin1234";

// NTP server settings
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 19800;  // GMT +5:30 for India in seconds
const int daylightOffset_sec = 0;  // No daylight saving in India

// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SDA_PIN 21
#define SCL_PIN 22

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Connection timeout
#define CONNECTION_TIMEOUT 10

// Declare timeinfo globally
struct tm timeinfo;

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

  // Initialize I2C with specific pins
  Wire.begin(SDA_PIN, SCL_PIN);

  // Initialize OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.clearDisplay();
  display.setTextColor(WHITE);

  // Connect to Wi-Fi with timeout
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("Connecting to WiFi...");
  display.display();

  int timeout_counter = 0;
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    timeout_counter++;
    if(timeout_counter >= CONNECTION_TIMEOUT*2) {
      Serial.println("Connection failed, restarting...");
      display.clearDisplay();
      display.setCursor(0, 0);
      display.println("WiFi connection failed");
      display.println("Restarting...");
      display.display();
      delay(2000);
      ESP.restart();
    }
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Connected!");
  display.println("Getting time...");
  display.display();

  // Initialize and get time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

  // Wait for time to be set
  int retry = 0;
  while(!getLocalTime(&timeinfo) && retry < 10) {
    Serial.println("Failed to obtain time");
    delay(1000);
    retry++;
  }

  if(retry >= 10) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("Failed to get time");
    display.println("Restarting...");
    display.display();
    delay(2000);
    ESP.restart();
  }
}

void loop() {
  if(!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(0, 0);
    display.println("Failed to get time");
    display.display();
    return;
  }

  // Format time as HH:MM:SS
  char timeString[9];
  strftime(timeString, sizeof(timeString), "%H:%M:%S", &timeinfo);

  // Format date as DD-MM-YYYY
  char dateString[11];
  strftime(dateString, sizeof(dateString), "%d-%m-%Y", &timeinfo);

  // Format day of week
  char dowString[10];
  strftime(dowString, sizeof(dowString), "%A", &timeinfo);

  // Display time on OLED
  display.clearDisplay();

  // Show time in large font
  display.setTextSize(2);
  display.setCursor(10, 5);
  display.println(timeString);

  // Show date in smaller font
  display.setTextSize(1);
  display.setCursor(20, 30);
  display.println(dateString);

  // Show day of week
  display.setCursor(20, 45);
  display.println(dowString);

  display.display();

  // Also print to serial monitor
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");

  delay(1000);
}

CONCLUSION

Throughout this project, I gained valuable hands-on experience in PCB design, component handling, and troubleshooting. I learned important lessons about proper desoldering techniques, especially for sensitive components like LEDs. The implementation of the OLED display and rotary encoders has enhanced my understanding of integrating different electronic components and programming them effectively.

FILES

GERBER

SCHEMATICS

Program


Last update: June 26, 2025