Skip to content

10-Output devices

Assignments

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 - Demonstrate workflows used in controlling an output

The road map i made for this week can be accesed here

tt

Group Assignment

You can access our group assignment here

Reflection

Individual Assignments

This is how the pcb turned out in the end.

scematic

Matrix Display Test

The first component I’ll be using is the matrix display so for the eyes of the bot. While testing and learning about this component, I was able to change up and make a few different animations!

heart

eye

The code for the blinking eye is from chatgpt.

#include <LedControl.h>

// DIN = 12, CLK = 11, CS = 10
LedControl lc = LedControl(12, 11, 10, 1);
byte eyeOpen[8] = {
  B01111110,
  B10000001,
  B10000001,
  B10111101,
  B10111101,
  B10000001,
  B10000001,
  B01111110
};

// Half-closed eye (top lid coming down) (vertical flipped)
byte eyeHalfClosed1[8] = {
 B00001110,
  B00110010,
  B01000010,
  B01111110,
  B01111110,
  B01000010,
  B00110010,
  B00001110
};

// Closed eye (vertical flipped)
byte eyeClosed[8] = {
  B00011000,
  B00011000,
  B00011000,
  B00011000,
  B00011000,
  B00011000,
  B00011000,
  B00011000
};

// Half-closed eye (bottom lid going up) (vertical flipped)
byte eyeHalfClosed2[8] = {
  B00001110,
  B00110010,
  B01000010,
  B01111110,
  B01111110,
  B01000010,
  B00110010,
  B00001110
};
void setup() {
  lc.shutdown(0, false);       
  lc.setIntensity(0, 8);       
  lc.clearDisplay(0);          
}

void loop() {
  displayFrame(eyeOpen);
  delay(6000);

  displayFrame(eyeHalfClosed1);
  delay(500);

  displayFrame(eyeClosed);
  delay(1000);

  displayFrame(eyeHalfClosed2);
  delay(500);
}

void displayFrame(byte frame[8]) {
  for (int i = 0; i < 8; i++) {
    lc.setRow(0, i, frame[i]);
  }
}

Code explaination

This Arduino sketch controls an 8x8 LED matrix using the LedControl library to display an eye animation. The matrix is driven by a MAX7219 chip connected through pins DIN=12, CLK=11, CS=10, with one device in the chain. Four frames are defined using 8-byte arrays: an open eye, two half-closed variations, and a closed eye, each represented by binary patterns where 1 lights up a pixel and 0 leaves it off. In the setup() function, the matrix is initialized, brightness set, and the display cleared. The loop() function then animates blinking by sequentially showing each eye frame with delays: open for 6 seconds, half-closed for 0.5 seconds, fully closed for 1 second, then half-closed again before reopening. The helper function displayFrame() updates the display row by row with the chosen frame’s pattern, creating the effect of a blinking eye.

This was just the test program i ran to get familar with this component. Later onwards, I combined the expressions with the movement(motor)and ultrasonic.

### Motor

I used two dc motors for my bot for the movement. The movemente i made with the motors include forward, backward turning and delay’s in between movements.

// Motor A control pins
const int in1 = 2;  // D2 on Xiao ESP32-C3
const int in2 = 3;  // D3

// Motor B control pins
const int in3 = 4;  // D4
const int in4 = 5;  // D5

void setup() {
  // Set all motor control pins as output
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  // Ensure motors are off at start
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}
void loop() {
  forward();
  left();
  backward();
  right();
  stop();
}
void forward(){
  // === FORWARD ===
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(100);  // short delay to avoid pin conflict
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);  // run forward for 2 seconds
}
void backward(){
  // === REVERSE ===
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(100);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);  // run reverse for 2 seconds
}
void right (){
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(100);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(2000);  // run reverse for 2 seconds
}
void left(){
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(100);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);  // run reverse for 2 seconds
}
void stop(){
  // === STOP ===
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(2000);  // pause for 2 seconds
}

Code explaination

This code controls two DC motors connected to an ESP32-C3 board using four GPIO pins (in1–in4), where Motor A uses pins 2 and 3 and Motor B uses pins 4 and 5. In the setup() function, all motor pins are set as outputs and initialized to LOW so the motors are off at startup. The loop() function repeatedly calls movement functions—forward(), left(), backward(), right(), and stop()—to make the motors move in sequence. Each movement function sets the pins HIGH or LOW in a way that determines the rotation direction of the motors: both forward for forward motion, both reversed for backward, one forward and one backward for turning, and all LOW for stopping. Short delays are added to avoid pin conflicts and to keep each motion running for about two seconds before switching.

After learning the code of the motors though, i needed to learn how to make the connections, which lead to alot of issues in the future(power connections done wrong)

While learning how to connect the motors to the motor driver with an arduino, i kept having issues where it would show me its connected, but the code didnt work.

connection1

After multiple failed attempts, i reached out to my instructor and got some resources and suggestions

digram

After looking carefully at this diagram, I realised

1- I got the ground and power connections with the motor driver and ardunio wrong. I was trying to connect the power of my arduino to the board, when it turns out only ground is required.

2- I inversed the ground and power of the motor driver. I honestly made this mistake many more times even after this. I almost burned a motor driver, and ended up frying 2 laptops.😅

3- that I conneced the motors inversed, so even after the motors were moving, the direction was mixed up.

While my instructor was explaining he drew this diagram to help me understand better.

diagram sketch

After i was able to understand all of this,I fixed all the connections and then tested it out with arduino.

connection2

Here an overview of all the components i learned working together!

components tgr

Oled

This is what i initally tryed out for this week before completing my final project, but i didnt test it out with my working mcb so it didnt fit the requirements for this weeks assignment. Even so heres a test out video.

working

/*
 Light meter
 Oled interfacing with Seeeduino XIAO
 Download Libraries:
 https://www.electroniclinic.com/arduino-libraries-download-and-projects-they-are-used-in-project-codes/

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// int LDR = A0; 
// int LDR_DATA; 

// int F_value; // LDR sensor final value

void setup() {

  Serial.begin(57600);
  // pinMode(LDR, INPUT); 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
}
void loop() {
//  F_value = readSensor();

  display.clearDisplay();

  // display R G B Values
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("L Meter:");

  // display.setTextSize(3);
  // display.setCursor(0, 28);
  // display.print(F_value);

  display.setTextSize(1);
  display.setCursor(0, 56);
  display.print("www.electroniclinic.com");

display.display(); 
}
// int readSensor()
// {
//   LDR_DATA = analogRead(LDR);
//   LDR_DATA = map(LDR_DATA,0,1023,0,100);
//   return(LDR_DATA);
//   delay(1000);  
// }

Errors

1- The first error I faced was checking the power with a multimeter. The first few times the connection was just not done right, but afterwards, even if i got the connection correct, it turned out that it just short circuited, so i needed to make a new microcontroller.

connection

2- During the connection process, i also, like i mentioned made quite a few wrong connections, so i burned up the oled.It didn’t exactly burn, it just heated up but i felt like i should mention it in my errors.

Thank you


Last update: October 4, 2025