Skip to content

Week 9. Output Devices

Group Assignment

Link to the group assignment: Bhutan group

Pointers:

  • Check the LEDS before using them with microcontrollers
  • Digital power supply is great as it shows both voltage and current

Individual Assignment

This week for Output Devices, we need to add an output device to the micro controller board that we have designed.

Since my final project is on fixed wing drones, I want to specifically focus on servo motors for the week (and if I have time move on to some other devices). I added a three pin connection on my board specifically with a servo motor in mind so thats what I will be working with this week.

Board with the servo:

Alt text

Servo up down using serial monitor

My actual goal was to write a code wherein I could hold u in the serial monitor and that would cause the motor to turn from 0 to 90 as I hold it to simulate it elevating and the opposite for if I hold d with that simulating descent. I was unable to do so no matter what I tried, even with the help of ChatGPT. My whole reasoning to use the serial monitor was so that it could act as a joystick (press u flick joystick up, press d flick it down. but it didn’t work)

So I decided to do something simplier where if you press u in the serial monitor, it will go to 90 degrees and if you press d, it will go to 0 degrees. A bit more extreme and less control than I wanted, but still a good exercise. I used ChatGPT to help with the code and I edited it a bit.

Code:


#include < Servo.h >

Servo myServo;  // create servo object

void setup() {
  Serial.begin(9600);  // initialize serial communication
  myServo.attach(9);  // attach servo to pin 9

  while(!Serial)
  Serial.println("u for up, d for down");
}

void loop() {
  if (Serial.available()) {  // check if there's incoming serial data
    char c = Serial.read();  // read the first byte of incoming data
    if (c == 'u') {  // if the character is 'u'
      myServo.write(-90);  // move the servo to -90 degrees
    } else if (c == 'd') {  // if the character is 'd'
      myServo.write(90);  // move the servo to 90 degrees
    }
  }
}

Servo control using Button

I wanted to do something similar to what I did previously. I wanted the motor to move to 90 when pressed and move back to 0 when there is no press. I did this to simulate an aircraft using its ailerons to lift for a short bit before returning back to its normal position.

I used ChatGPT for this code as well because I tried to tweak the sweep example but I was unsuccessful.

Code:

#include < Servo.h >

Servo myServo;  // create a servo object

int buttonPin = 3; // the pin number of the button
int buttonState;   // variable to store the button state
int servoPos = 0;  // variable to store the servo position

void setup() {
  myServo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin, INPUT);  // sets the button pin as input
}

void loop() {
  buttonState = digitalRead(buttonPin); // read the button state

  if (buttonState == HIGH) {  // if the button is pressed
    myServo.write(90);  // rotate the servo to 90 degrees
    delay(15);  // delay for the servo to reach the desired position
  } else {  // if the button is not pressed
    myServo.write(0);  // rotate the servo back to 0 degrees
    delay(15);  // delay for the servo to reach the desired position
  }
}

Design Files:

Arduino Codes zipped

Bhutan Board Kicad sch,pcb + png