Skip to content

12. Output devices

I’m using this week to learn about driving motors and servos with the ATtiny44. In the coming weeks I am planning to use a verson of the board to drive a motor in my final project and potentially on the group project.

Files

Serial controlled Servo Board: Eagle: Schematic | Board

Design Process

I’m basing my design on on Neil’s hello.servo.44 board . The main shift is that I’m going to add an FTDI 6 pin connector to the board so I can have pins for TX/RX communication. These pins should allow me to send serial commands to the board to control the servo motors via the computer. Eventually this could be a meathod to control servos via streaming data.

Using the ATtiny Datasheet

Because my goal is to add two TX/RX pins to control servo motors I needed to double check which pins are PWM enabled. I considered switching this pins from Neil’s board to make layout easier with the FTDI connector. After looking at the datasheet I realized that pins 6 and 7 that Neil used are 2 of the 4 PWM pins and the other two are not better located. So, PA0 and PA1 are the pins I’m going to use for the TX/RX pins.

I designed the board in Eagle and started by adding all the parts from Neil’s board to the schematic. Then, I added the 6 pin connector for the FTDI. I connected the TX and RX pins to PA0 and PA1. The + & - pins on the FTDI connector were also connected. This will allow the board to be powered by the FTDI connector if necessary.

The traces were pretty easy to route and after I rotated the FTDI connector I was able to route the wires without any need to overlap and vias.

Before I exported the CAM files for the CNC Mill I adjusted the dimensions of the board and switched the FTDI pins from surface mount pads to through hole pads. I think through hole for the connector is a good move because it can handle the stress of plugging and unplugging the cable over and over.

Fabrication Process

After importing the CAM files into CopperCAM and prepping to mill like described in week 5

Problem with Board Export

I noticed that the simulation of milling with the 1/64” bit there is not enough space between the bottom middle pad and a trace. They are connected but should be seperated. To solve this problem I shrank the pad by about 2% and re-ran the simulation.

The board came out looking really nice with no noticable flaws.

I located all the parts and put them on paper with double sided tape to allow me to see everything and not lose components.

Problem with Voltage Regulator

As I was stuffing the board I ran into issue. The board calls for a 5v regulator but Fab Lab Houston does not have the correct size component. The component the fab lab has is 3.3volt instead of 5.

To fix this problem I decided to bypass the voltage regulator by connecting the left voltage in pad to the center voltage output. I will only be able to run it with a 5 volt DC powersupply because anything higher could ruin the board. This should work and I’ll have to order the correct 5 volt regulator for down the road.

Programming

With the board milled and assembled I flashed the ATtiny44 chip like explained in Week 9 . I thought I would be able to combine elements from the software serial code from week 9 and the Arduino servo libary

Problem with Servo Library

When I uploaded the servo example from Arduino to test the board the servos switched but didn’t move smoothly. I decided to do some top down trouble shooting and try to upload Neil’s code written in C. This code worked so I figure there the servo library must be to bulky for the ATtiny44. I found a forum where people discussed ways to control servos without the library. The code from there did move the servos perfectly.

Serial Control

Once I had the servos moving I moved on to the serial communication. I stated with the software serial program from Week 9 to test the TX/RX communication via the FTDI to USB cable. In the code below you can see I added the software serial library at the top, assigned the TX/RX pins, and added a serial.printLn to each of the functions that move the servos. Then, in the serial monitor I could see the word “forward” and “reverse” when the motors moved.

This Tutorial did a good job of describing how to use a char variable to store a character from the serial monitor. Basically, at the top of each loop the code below reads the serial and shows what ever comes across in the rxChar variable. Then, I wrote two if statements that looks for specific characters. If there is an “a” store the if statment triggered moved the servos forward. If there is a “b” that comes across the if statement that is triggered moves the motors backwards.

Serial Controlled Servo Code

#include <SoftwareSerial.h>
#define RX    0   //  Pin 0
#define TX    1   //  Pin 1

char rxChar= 0; 

int servo = 7;      // Pin 7
int servo2 = 6;     // Pin 6

int angle;
int pwm;

SoftwareSerial Serial(RX, TX);

void setup()
{

  Serial.begin(9600);
  Serial.println("Initializing...");

 pinMode(servo, OUTPUT);
  pinMode(servo2, OUTPUT);
}

void loop ()
{

if (Serial.available() >0){          // Check receive buffer.
    rxChar = Serial.read();            // Save character received. 
    Serial.flush();                    // Clear receive buffer.
  if (rxChar == 'a'){
 for (angle = 0; angle <= 140; angle += 5)  {
   servoPulse(servo, angle);
   servoPulse(servo2, angle);  
   Serial.println("forward");
   }}

   if (rxChar == 'b'){
 for (angle = 140; angle >= 0; angle -= 5)  {
   servoPulse(servo, angle); 
   servoPulse(servo2, angle);  \
   Serial.println("backward");
   }
   }
}
}

void servoPulse (int servo, int angle)
{
 pwm = (angle*11) + 500;      // Convert angle to microseconds
 digitalWrite(servo, HIGH);
 digitalWrite(servo2, HIGH);
 delayMicroseconds(pwm); //make the angle conversion to microseconds the delay between pulses
 digitalWrite(servo, LOW);
 digitalWrite(servo2, LOW);
 delay(50);                   // Refresh cycle of servo
}