Week 8 - Output Devices

Posted by Jordan Kennedy on November 7, 2017

The task for this week was to create an output device. I chose to program a motor. This is to compliment my final project.

Shown below is my generated toolpath for the milling of my PCB.

This was a fairly straightforward process building upon our previous weeks of creating circuits.


My board was slightly bigger than my previous cuts. I had to put a much larger board on.


Shown here is my final board with all the required components soldered on.


When I plugged in a battery for the first time, I plugged it in backward and shorted my H-bridge. This took me quite a while to figure out. I ended having to replace the H-bridge.


Here is my plugged in board with battery and motor.

Plugged In

This project was a ridiculous amount of trouble shooting for something quite trivial.

Plugged In

Modified Motor Board

To modify the DC motor board I added a button and resistor as seen below.


I wanted to program the board such that every time I pressed the button the motor would change speeds.

So I took Neil’s DC motor code and modified so that the motor only moved in one direction and that it took an input from the button.

Shown below is my board.



Shown is a video of the changing speeds as a result of a button push.


Below is my modified code:

// hello.H-bridge.44.DC.c
// H-bridge DC motor hello-world
// Neil Gershenfeld (modified by Jordan Kennedy)
// 12/12/2017
// (c) Massachusetts Institute of Technology 2012
// This work may be reproduced, modified, distributed,
// performed, and displayed for any purpose. Copyright is
// retained and must be preserved. The work is provided
// as is; no warranty is provided, and users accept all 
// liability.

#include <avr/io.h>

#include <util/delay.h>

#define output(directions,pin) (directions |= pin) // set port direction for output

#define input(directions,pin) (directions &= (~pin)) // set port direction for input

#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 on_delay() _delay_us(4) // PWM on time

#define fast_off_delay() _delay_us(1) // PWM fast off time

#define medium_off_delay() _delay_us(3) // PWM medium off time

#define slow_off_delay() _delay_us(5) // PWM slow off time

#define long_delay() _delay_us(1000000) // Long Delay for button hold

#define PWM_count 20000 // number of PWM cycles

#define cycle_count 10 // number of speed cycles

#define bridge_port PORTA // H-bridge port

#define bridge_pin PINA // H-bridge pin

#define bridge_direction DDRA // H-bridge direction

#define IN1 (1 << PA3) // IN1

#define IN2 (1 << PA2) // IN2

#define BUTTON (1 << PA7) // IN2

#define lowSpeed = 0

#define midSpeed = 1

#define highSpeed = 2

int motorDelay(int selection)
  int err = 0;
    case 0: slow_off_delay(); break;
    case 1: medium_off_delay(); break;
    case 2: fast_off_delay(); break;
    case 3: break;
    default: err = 1; break;
  return err;

int main(void) 
   // main
   static uint16_t count;
   static uint8_t cycle;
   // set clock divider to /1
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   // initialize H-bridge pins
   clear(bridge_port, IN1);
   output(bridge_direction, IN1);
   clear(bridge_port, IN2);
   output(bridge_direction, IN2);

   // initialize button pin
   clear(bridge_port, BUTTON);
   input(bridge_direction, BUTTON);
   // for now just spin forward
   clear(bridge_port, IN1);
   set(bridge_port, IN2);
   // start with motor in low speed
   int speed = 0;

   // set button state
   int buttonState = 1;
   // main loop
   while (1) 
      set(bridge_port, IN2);
      clear(bridge_port, IN2);
      buttonState = (bridge_pin &= BUTTON) >> PA7;
      if(buttonState == 0)
        speed += 1;
        if (speed > 3)
          speed = 0;