Skip to content

12. Output devices

On this week, I learned how to creaate electrical boards for motors.

Group assignment

The detailed works of our group assignment are shown here.

Individual assignment

The goal of the individual assignment on this week was add an output device to a microcontroller board you’ve designed, and program it to do something. Also this week, I would like to work this assignment as preparation for my final project. At that time, I planned to use servo motors for my final project. Then, I tried to make two boards to control servo motors.

Servo Motor

In general, the rotational angles of servo motors are controlled based on PWM values.

Designing board

To design an electrical board for a servo motor, first I referred the sample schematic given in class. In the sample board, there was no serial communication connection. However, serial communication was necessary for my individual purpose, so I added the connection for serial communication.

After adding the connection and seeing the sample board again, I realized other issues. In the sample board, ATtiny 44 and servo share the same power supply due to no serial connection. However, as long as the serial connection in my board also gave power supply to the ATtiny44, the multiple power supplies were given to the ATtiny44. I worried about whether this power structure was well or not, so I asked my local instructor and professional electrical engineer who is a member of Fablab Kannai. They said I had to separate power supplies for a serov and ATtiny44. Then, I changed my schematic design like below figure.

Most of the components were the same as previous assignments. New components are
- Regulator_SOT223
- Jack-2.1MM

For this schematic design, when I typer “ERC”, some errors about that pin connection is not enough. However, this was why I choose little different kinds of the regulator. Then, this error can be ignored.

After designing the schematic, I also made paths on my PCB board as usual.

Programming codes

#include <avr/io.h>    
#include <util/delay.h>    

#define output(directions,pin) (directions |= pin) // set port     direction for output
#define set(port,pin) (port |= pin) // set port pin   
#define clear(port,pin) (port &= (~pin)) // clear port pin    
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define position_delay() _delay_ms(1000)

#define PWM_port PORTA
#define PWM_pin (1 << PA6)
#define PWM_direction DDRA

int main(void) {
   // main
   // set clock divider to /1
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   // set up timer 1
   TCCR1A = (1 << COM1A1) | (0 << COM1A0); // clear OC1A on compare match
   TCCR1B = (0 << CS12) | (1 << CS11) | (0 << CS10) | (1 << WGM13); // prescaler /8, phase and frequency correct PWM, ICR1 TOP
   ICR1 = 25000; // 20 ms frequency
   // set PWM pin to output
   clear(PWM_port, PWM_pin);
   output(PWM_direction, PWM_pin);
   // main loop
   while (1) {
      // 1 ms PWM on time
      OCR1A = 1250;
      // 1.5 ms PWM on time
      OCR1A = 1875;
      // 2 ms PWM on time
      OCR1A = 2500;

The program code makes one servo motor rotate by three kinds of angles based on inputted PWM values. The below video demonstrates the servo motor could rotate by three angles.

EAGLE files
Schematic file
Board file

PNG files
Circuit file
Outline file

C language file
Make file