Skip to content

Week 10. Output Devices

Hero Shot

After completing Input devices week, since this week being output devices we were involved in exploring different output devices in group and for Individual Assignment I explored on servo motor and speaker. As both of them are required during my final project so for that reason, I thought why not I just give a try on exploring and familiarising them before I actually starts working with my final project.

Assignments for the Week

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.

Link to the group Assignment is here

key takeaways from this week

  • While giving the external power from DC power supply for the devices that we are working with, we should adjust and set the value of voltage and current equall to or little bit more than the nominal values of devices.

  • Ensure to provide proper connections and ground connections while using the DC power supply.

  • Never connect any devices with your microcontroller and laptop without referring to its datasheet because it is very important that we know how much current is being drawn by each components. This is mainly to avoid short circuiting of microcontroller board and to prevent laptop from going dead due to instant power drain by the devices.

  • I also had similar experience of directly connecting servo motor through laptop and while giving supply, my laptop died and I had to open it and remove the battery jack and reconnect it, then only it started working.

Individual Assignment:

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

This week being the output devices, I tried to explore on servo motor and the speaker (DF player) because I will be using both of them for my final project.

So for that I tried to connect both output devices with arduino board and with my own micro controller board which I had made during electronics design week for which the link is given here

MG996R

I started setting up the servo motor MG996R by making necessary connections. Since this set up was conducted in group I included this in my invidual assignment as well and apart from that I also explored on Micro Servo 9g SG90 servo motor.

I had attached code below to do programing for the servo motor and for the code I had reffered from ChatGpt.

      #include <Servo.h>

      Servo servo;  // create servo object to control a servo

      void setup() {
      servo.attach(9);  // attaches the servo on pin 9 to the servo objectư
      servo.write(0);   // rotate slowly servo to 0 degrees immediately
      }

      void loop() {
      for (int angle = 0; angle <= 180; angle += 1) {  // rotate slowly from 0 degrees to 180 degrees, one by one degree
      // in steps of 1 degree
      servo.write(angle);  // control servo to go to position in variable 'angle'
      delay(10);         // waits 10ms for the servo to reach the position
      }

      for (int angle = 180; angle >= 0; angle -= 1) {  // rotate from 180 degrees to 0 degrees, one by one degree
      servo.write(angle);                        // control servo to go to position in variable 'angle'
      delay(10);                               // waits 10ms for the servo to reach the position
        }
      }

The voltage and current drawn by MG996R during operation was recorded as;

  • Voltage: 6:00V

  • Ampere: 0.08A

Micro Servo 9g SG90

Next I went for Micro servo 9g SG90 in which I had made connection by giving the external power supply from DC power supply and signal pin connecting to microcontroller board.

The attached code below is used for programing the servo motor at certain angles and for the code I had referred to ChatGpt and Prompt I used was; Can you generate the code to rotate the servo motor at certain angles starting from 0 to 180

 #include <Servo.h>

 Servo myservo;  // creating Servo motor to sweep in segmented angles
 int sweepAngles[] = {0, 55, 110, 165, 180};  // Key angles are maintained so that I can rotate the servo motor as per my convenient
 int currentIndex = 0;
 bool forwardSweep = true;  // Direction flag

 void setup() {
 myservo.attach(9);  // Servo on pin 9
 }

 void loop() {
 // Move to the next target angle
 int targetAngle = sweepAngles[currentIndex];
 smoothSweep(myservo.read(), targetAngle);

 delay(2000);  // Pause for 2 sec

 // Update direction (forward or reverse)
 if (forwardSweep) {
 if (currentIndex < 4) currentIndex++;  // Move to next angle
 else { 
      forwardSweep = false;  // Reverse direction after 180°
 }
 } else {
 if (currentIndex > 0) currentIndex--;  // Move to previous angle
 else { 
      forwardSweep = true;  // Forward direction after 0°
 }
 }
 }

 // Smooth sweep between two angles
 void smoothSweep(int startAngle, int endAngle) {
 int step = (startAngle < endAngle) ? 1 : -1;  // Direction
 for (int pos = startAngle; pos != endAngle; pos += step) {
 myservo.write(pos);
 delay(10);  // Adjust speed (smaller = faster)
 }
 myservo.write(endAngle);  // Ensure final angle is precise
 }

The voltage and current drawn by Micro servo 9g SG90 during operation was recorded as;

  • Voltage: 6:00V

  • Ampere: 0.07A

Trying servo motor with my own microcontroller board

After I finished programing servo motor with arduino board I tried to do programing with my own board.

Attached below is the code which I used for programing servo motor and the prompt which I used was; “Can you generate cpp code for arduino IDE comprising of XIAO RP2040 as a microcontroller board with servo motor SG90 with its signal pin connected to D4 of microcontroller board. And the program to run should include that the motor should rotate at full speed clockwise for 2 seconds, stop for 1 second and again rotate anticlockwise for 2 seconds and stop for 1 second with continuous cycle.

 #include <Servo.h>

 Servo continuousServo;
 const int servoPin = D4;

 void setup() {
 continuousServo.attach(servoPin, 500, 2400);
 Serial.begin(9600);
 Serial.println("Continuous rotation servo test");
 }

 void loop() {
 // Full speed one direction
 continuousServo.write(0);
 Serial.println("Full speed clockwise");
 delay(2000);

 // Stop
 continuousServo.write(90);
 Serial.println("Stopped");
 delay(1000);

 // Full speed other direction
 continuousServo.write(180);
 Serial.println("Full speed counter-clockwise");
 delay(2000);

 // Stop
 continuousServo.write(90);
 Serial.println("Stopped");
 delay(1000);
 }

Speaker (DF mini player)

After exploring the servo motor next I went for exploring the speaker as well as for my final project as it plays a key role. So for that purpose in order to make sure that I give proper connection for my device, I had referred to its datasheet and pinout diagram.

I connected the RX pin of DF player through 1K resistor to pin 11 0n arduino board and the TX pin of DF player to pin 10 of Arduino board with speaker. After that I uploaded the Mp3 file into the SD card and then inserted inside the DF player.

Next I opened the Arduino IDE and ran the example code from there by following the steps mentioned below;

The attached code below is used for programing the DF player for playing MP3 file

 #include "Arduino.h"
 #include "DFRobotDFPlayerMini.h"

 #if (defined(ARDUINO_AVR_UNO) || defined(ESP8266))   // Using a soft serial port
 #include <SoftwareSerial.h>
 SoftwareSerial softSerial(/*rx =*/10, /*tx =*/11);
 #define FPSerial softSerial
 #else
 #define FPSerial Serial1
 #endif

 DFRobotDFPlayerMini myDFPlayer;
 void printDetail(uint8_t type, int value);

 void setup()
 {
 #if (defined ESP32)
 FPSerial.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
 #else
 FPSerial.begin(9600);
 #endif

 Serial.begin(115200);

 Serial.println();
 Serial.println(F("DFRobot DFPlayer Mini Demo"));
 Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

 if (!myDFPlayer.begin(FPSerial, /*isACK = */true, /*doReset = */true)) {  //Use serial to communicate with mp3.
 Serial.println(F("Unable to begin:"));
 Serial.println(F("1.Please recheck the connection!"));
 Serial.println(F("2.Please insert the SD card!"));
 while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
 }
 }
 Serial.println(F("DFPlayer Mini online."));

 myDFPlayer.volume(30);  //Set volume value. From 0 to 30
 myDFPlayer.play(1);  //Play the second mp3
 }

 void loop()
 {
 static unsigned long timer = millis();

 if (millis() - timer > 60000) {
 timer = millis();
 myDFPlayer.next();  //Play next mp3 every 1 second.
 }

 if (myDFPlayer.available()) {
 printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
 }
 }

 void printDetail(uint8_t type, int value){
 switch (type) {
 case TimeOut:
      Serial.println(F("Time Out!"));
      break;
 case WrongStack:
      Serial.println(F("Stack Wrong!"));
      break;
 case DFPlayerCardInserted:
      Serial.println(F("Card Inserted!"));
      break;
 case DFPlayerCardRemoved:
      Serial.println(F("Card Removed!"));
      break;
 case DFPlayerCardOnline:
      Serial.println(F("Card Online!"));
      break;
 case DFPlayerUSBInserted:
      Serial.println("USB Inserted!");
      break;
 case DFPlayerUSBRemoved:
      Serial.println("USB Removed!");
      break;
 case DFPlayerPlayFinished:
      Serial.print(F("Number:"));
      Serial.print(value);
      Serial.println(F(" Play Finished!"));
      break;
 case DFPlayerError:
      Serial.print(F("DFPlayerError:"));
      switch (value) {
      case Busy:
           Serial.println(F("Card not found"));
           break;
      case Sleeping:
           Serial.println(F("Sleeping"));
           break;
      case SerialWrongStack:
           Serial.println(F("Get Wrong Stack"));
           break;
      case CheckSumNotMatch:
           Serial.println(F("Check Sum Not Match"));
           break;
      case FileIndexOut:
           Serial.println(F("File Index Out of Bound"));
           break;
      case FileMismatch:
           Serial.println(F("Cannot Find File"));
           break;
      case Advertise:
           Serial.println(F("In Advertise"));
           break;
      default:
           break;
      }
      break;
 default:
      break;
 }

 }

Trying DF mini player with my own microcontroller board

Secondly I went on playing MP3 using DF mini player by connecting it to my own micro controller board.

Attached below is the code which I used for programing DF mini player with my board(XIAO RP2040) and the prompt which I used was; “Can you generate cpp code for DF Mini player with XIAO RP2040 as a micro controller board to play MP3 from the SD card. I wanted to keep A2 pin of XIAO RP2040 as RX and A1 pin of XIAO RP2040 as TX.

 #include <SoftwareSerial.h>
 #include <DFRobotDFPlayerMini.h>

 // Create SoftwareSerial on pins A2 (RX) and A1 (TX)
 SoftwareSerial mySerial(A2, A1);  // RX, TX
 DFRobotDFPlayerMini myDFPlayer;

 void setup()
 {
 Serial.begin(9600);
 mySerial.begin(9600);  // DFPlayer baud rate

 Serial.println("Initializing DFPlayer...");

 if (!myDFPlayer.begin(mySerial)) {
 Serial.println("Unable to begin:");
 Serial.println("1. Please recheck the connection.");
 Serial.println("2. Insert an SD card.");
 while (true);
 }

 Serial.println("DFPlayer Mini online.");

 myDFPlayer.volume(20);  // Volume: 0~30
 myDFPlayer.play(1);     // Play the first MP3 file on the SD card
 }

 void loop()
 {
 // Nothing to do here unless you want to add controls
 }
  • Files for the week is attached here

  • Code for running servo motor by using own board is attached here

  • Code for playing MP3 using DF mini player with my own board is attached here

Thank you! It was great learning the Out put devices and experimenting it with our own customised board.