Fab Academy 2024 Website

  • Home
  • About
  • WEEK1 Project Management / Principles and Practices
  • WEEK2 Computer Aided Design
  • WEEK3 Computer-controlled cutting
  • WEEK4 Electronics production
  • WEEK5 3D scanning and printing
  • WEEK6 Embedded programming
  • WEEK7 Computer-controlled machining
  • WEEK8 Electronics design
  • WEEK9 Output devices
  • WEEK10 Mechanical design, Machine design
  • WEEK11 Input devices
  • WEEK12 Molding and casting
  • WEEK13 Networking and communications
  • WEEK14 Interface and application programming
  • WEEK15 Wildcard week
  • WEEK16 System integration
  • WEEK17 Applications and implications, project development
  • WEEK18 Invention, intellectual property, and income
  • Final
目次
  • WEEK9 Output devices
    • Group Assignment
    • Individual Assignment
      • Servo Motor SG90
      • Controlling servo motor 1
      • Controlling servo motor 2
        • ChatGPT Prompt

WEEK9 Output devices

  1. Fab Academy 2024 Website
  2. WEEK9 Output devices
hito |


WEEK9 Output devices

Group Assignment

measure the power consumption of an output device
Group assignment Page

Individual Assignment

add an output device to a microcontroller board you’ve designed, and program it to do something

In this assignment, I will control a servo motor connected to a test board I made.

Servo Motor SG90

The SG90 is a servo motor that can rotate approximately 180 degrees and is composed of a DC motor, a positioning system, and gears. It has three wires: the red wire is connected to the power supply, the brown wire is connected to the ground, and the yellow wire is connected to the PWM (Pulse Width Modulation) signal.

SG90 datasheet

alt text alt text

alt text

Controlling servo motor 1

Download sample program:
File->Examples->Servo->Sweep

I updated the pin number in the source code from 9 to 6 to reflect the servo being connected to pin 6.

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
    myservo.attach(6);  // attaches the servo on pin 6 to the servo object    
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}
Success!!


Controlling servo motor 2

A code to control the rotation amount of a Servo Motor.

Original Code

import time

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

GPIO.setup(4, GPIO.OUT)

p = GPIO.PWM(4, 50)

#Duty Cycle 0%
p.start(0.0)

while True:
    print("input Duty Cyle (2.5 - 12)")
    dc = float(input())

    #DutyCycle dc%
    p.ChangeDutyCycle(dc)

    time.sleep(0.4)

    p.ChangeDutyCycle(0.0)

ChatGPT Prompt

Please convert the above code to Arudino programming language.

#include <Servo.h>

Servo myservo;  // Create a servo object

void setup() {
  myservo.attach(6); // Attach the servo motor to GPIO 6
  myservo.write(0);  // Set initial position to 0 degrees
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
  if (Serial.available() > 0) {
    float pos = Serial.parseFloat(); // Read input from serial and convert to floating point
    // Clear remaining characters in serial buffer
    while(Serial.available() > 0) {
      Serial.read();
    }

    if (pos >= 2.5 && pos <= 12) {
      // Manually convert duty cycle to angle
      float angle = (pos - 2.5) * (180 - 0) / (12 - 2.5);
      myservo.write((int)angle); // Rotate the servo motor to the specified angle
      Serial.print("Moved to: ");
      Serial.println(angle);
    } else {
      Serial.println("Input out of range");
    }
    delay(400); // Wait for 0.4 seconds
  }
  // For stabilization after movement
  myservo.write(0); // Return servo motor to 0 degrees position (if necessary)
  delay(100); // A short delay for stabilization
}
The Servo Motor will rotate accordingly base on the numbers entered from the Serial Monitor between 2.5 and 12.

My board design file

Copyright 2024 Hajime Ito - Creative Commons Attribution Non Commercial

検索
キーワードを入力して検索を始めましょう