Servo

As part of my final project I want to use a servo to open the lid of the catbowl automatically. It was obvious, therefore, to use this week to learn how to control a servo.

I wanted to get back into Eagle, so I took the template hello.servo.44 board design and added an LED. The LED was purely to show when my board was being powered, and the rest of the board was the same as the template. I forgot how difficult routing was but it was a good task to keep learning new strategies for component orientation.

On the template there is a 22uf capacitor as one of the components. After looking through the inventory of the electronics lab, it was clear that we did not stock these particular capacitors or those of higher capacitance. Thanks to Dan's suggestion and looking up basic properties of capacitors I decided to stack 3 10uf capacitors instead. As long as the capacitance is greater than or equal to 22uf the board should work fine. Furthermore, capacitors in parallel is a simple addition of individual capacitances.

As I wasn't quite sure about how to power the board or how to connect the servo properly, I decided to just double check that the board was being powered correctly. I used a power source and just touched the positive and negative leads to the voltage in and ground pins on my board. The light lit up so I was relieved that the board fabrication went alright.


//
// hello.servo.44.c
//
// servo motor hello-world
//
// set lfuse to 0x7E for 20 MHz xtal
//
// Neil Gershenfeld
// 4/8/12
//
// (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
#include

#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;
position_delay();
//
// 1.5 ms PWM on time
//
OCR1A = 1875;
position_delay();
//
// 2 ms PWM on time
//
OCR1A = 2500;
position_delay();
}
}

From there I programmed the board. I never configured my computer properly for programming directly through the terminal so I just copied and pasted the template C file into the arduino IDE to compile. It worked! Now I'd like to experiment a bit with the parameters according to what I want for my final project.

Speaker

My final project idea also incorporates some sort of feedback for the cat who is not supposed to eat from the bowl. For this I figured a loud beeping would suffice. So I've decided to also try the speaker output device.

For this board I'll simply be using the template for the hello.speaker.45 board. (Eventually I will integrate all of these components into one board, but that will be my electronics fabrication for the final project.)

During fabrication I had some issues with fab modules and connections to the roland mill. The USB0 connection was not found. The connector somehow got changed from the correct connection so once that was changed I could see the USB0 port and the roland mill was talking to fab modules. However, part of the end of the command line had been changed and so the machine did some abstract art on my stock instead of milling the traces. Once I changed the command line back to default everything went swimmingly.

I was impressed with how small this board and its components were. The mosfet and regulator were a little difficult to solder just because they were so tiny, but thanks to all the practice it was not a total nightmare. I'm starting to really enjoy soldering too which is nice.


//
// hello.speaker.45.c
//
// speaker PWM hello-world
// square wave, pseudo-random frequency
//
// Neil Gershenfeld
// 11/14/10
//
// (c) Massachusetts Institute of Technology 2010
// 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
#include

#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 cycle_delay() _delay_us(2) // cycle delay
#define current 150 // PWM current
#define off 255 // PWM off

#define MOSFET_pin (1 << PB1)
#define MOSFET_port PORTB
#define MOSFET_direction DDRB

int main(void) {
//
// main
//
static uint16_t cycle,cycles,delay,bit,lfsr;
//
// set clock divider to /1
//
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
//
//
//
TCCR0A = ((1 << COM0B0) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00)); // set OC0B on compare match and set fast PWM mode, 0xFF TOP
TCCR0B = (1 << CS00); // set timer 0 prescalar to 1
//
// initialize MOSFET pin
//
clear(MOSFET_port, MOSFET_pin);
output(MOSFET_direction, MOSFET_pin);
//
// initialize LFSR
//
lfsr = 1;
//
// main loop
//
while (1) {
//
// update 11 bit LFSR
//
bit = ((lfsr >> 0) ^ (lfsr >> 9)) & 1;
lfsr = (lfsr >> 1) | (bit << 10);
//
// loop over cycles
//
cycles = (1 << 15) / lfsr;
for (cycle = 0; cycle < cycles; ++cycle) {
//
// set PWM current on and delay
//
OCR0B = current;
for (delay = 0; delay < lfsr; ++delay)
cycle_delay();
//
// set PWM current off and delay
//
OCR0B = off;
for (delay = 0; delay < lfsr; ++delay)
cycle_delay();
}
}
}

Again I programmed with my FabISP and used the template code to program. My speaker has a lot more noise than the video example online, I'm not sure if this is due to the speaker itself or the connections on the board. I still want to look into that before choosing a speaker for my final project. As much as I love a dialup sound I think for my final project I'll just end up making a constant loud beep. All in all, this week was successful in preparing me and quelling my fears of how my final project will take shape. WOOHOO!

IR LED

The third output device I wanted to make was a small LED Remote to mimic the unique cats when they would want to try and eat from the cat bowl. The IR LED is not much different from a regular LED except it's much harder to know whether or not its working.

This time around I was feeling a little time crunched and tried to use autorouter in eagle to make my traces, unfortunately this meant that I was not paying attention to some main design rules. Somehow the trace for ground was connected to VCC. I ended up just cutting away with it with a razor but I'll be sure to manually route my boards from here on out, not worth that terrifying moment. Unfortunately, the traces were really messy and even after using a straight edge to score them it wasn't looking all that great. I'd like to figure this out, it seems my boards get progressively worse every week in terms of trace quality which isn't too good.

For a while I was trying to use the camera on my iphone (the back camera) to see whether or not the LED was lighting up, and it seemed like it wasn't lighting up. Although the data sheets for the LED said it required a 330 Ohm resistor before it, but after looking up some tutorials I found that a 100 Ohm would probably work better. Similarly, using the multimeter the amount of power going through the LED was below the rating of the LED. After some changing of resistors and changing code etc. the LED still wasn't working. So I looked it up on the internet, turns out that iPhone 5s' back camera has an IR filter, no wonder I couldn't see it, BUT the selfie camera works! Thus the beautiful photo above.

Unfortunately, the switches weren't working too well, it also seemed like there might be a connection between the outer copper and one of the LED traces as it kept turning on every time I touched part of the copper. If I choose to move forward with different frequencies of IR light I'll need to make sure this board is really well made and a little more robust. The code itself was pretty much the same as the switch and LED from previous weeks. This worked out pretty well but I'd like to make sure that it is powerful enough for the receiver as well. This will come next week with input devices.

Tools used this week: Eagle, Roland Mill, Arduino IDE, Fab ISP, Servo, Speaker, Solder, IR LED, iPhone 5s Selfie Camera