Tuesday, January 29, 2013

Hacking a Hexbug

I wasn't totally sure of all the things I wanted my toy to do by the time I finished hacking it, but I knew I wanted fine motor control. Unfortunately, this bug mechanically cannot turn left or right since its wheels are all locked together by their axles, and it only has one motor.
In any case, I should be able to reverse the motor electronically. For that, I needed the H-bridge. It's got six ports: four switch inputs, and two outputs for the motor. I've got the setup as follows:
switch1: controller
switch2: controller
switch3: ground
switch4: power*
output1: motorPower
output2: motorGround

The controller needs only to switch output voltage between switch1 and switch2 in order to change motor direction. That makes my controller program pretty simple.
*I am going to route power from the battery through a transistor directly controlled by my controller. This gives the controller control over all the current that gets to the motor.
So I made a quick test program that ran the motor back and forth.

The green board is the H-bridge I made earlier. Motor output looks strong!
The last part of the video is explained below.

Next, I wanted some input. I wanted to use the original touch sensors from the Hexbug, but when I soldered wires to them, they stopped working. It was frustrating, but I decided to scrap them. I only have about to week to finish this project, so I went straight to a sensor I've already gotten working - the LDR. (light-dependent resistor)
Seeing as insects are pretty sensitive to light, I figured light sensors would be a good fit. I planned for it to have one LDR on each side, so it would run away from wherever it detected a change in light.
My program simply sets a variable "lightLevel" for each sensor upon setup, then checks every 1/10 second whether the sensor reads differently up to some threshold I choose.

int lightLevelAhead = analogRead(frontLightSensor);
int lightLevelBehind = analogRead(backLightSensor);


boolean lightAheadChanged(){
  boolean didChange = (abs(analogRead(frontLightSensor) - lightLevelAhead) > 50);
  lightLevelAhead = analogRead(frontLightSensor);  //reset comparison variable
  return didChange;
}
boolean lightBehindChanged(){
  boolean didChange = (abs(analogRead(backLightSensor) - lightLevelBehind) > 50);
  lightLevelBehind = analogRead(backLightSensor); //reset comparison variable
  return didChange;
}

My threshold is 50 in this case. The lower the threshold, the more sensitive it is.

Using this code, I have the bug run its motors for a second when light changes - in either direction, depending on which LDR detected change.
I also decided to add a piezo sounder and LED for some audio and visual feedback.


I use my finger to cast a shadow over the LDRs.
The piezo just plays a couple notes in the direction it's running.

So far so good, but this won't work at all as a toy if it's stuck on USB power. I needed to figure out how to get the controller chip working without that red launchpad board. Thanks to YouTube, I found out that the chip runs almost exactly as you would expect, with battery power and ground on pins 1 and 20, but it also needs a 10K resistor connecting power to the reset pin.
I could mount the chip on that white breadboard, but that's a pretty big chunk of plastic for such a small bot. And with one weekend left to finish, I wanted to finalize my design. 
Fortunately, my brother happened to have a flat breadboard that I can cut to size. This breadboard requires soldering, so I needed an IC socket to mount the chip on so it can be removed again.
One trip to Radioshack later, I got started transferring my design to a more permanent form. 

That's the chip in the middle, sitting in its IC socket.

 Lookin' good!

I planned to have the H-bridge sandwich itself between the board and the bug, so I left room for the H-bridge wires to plug directly into it through six holes in the board. That also gives the H-bridge some rigidity, so I don't have to worry about it falling down. I thought it was clever.

Nightmare-fuel.

I have a friend who's actually educated in music composition, so I had him over to help me program a song I like into the controller. I'll have it play on setup.
I turn it on and...

The song's great, but there's clearly something wrong with the power, since the program is rebooting when it tries to power the motor. The board also seems to be shutting off randomly during testing.
I spent more time than I care to mention trying to make sure the batteries were making good connection with the board. I even found that my power switch was malfunctioning, so I added another and moved the connection to that one. 
I also added more voltage to the power by using a couple half-height button-cell 1.5V batteries in place of a single 1.5V. That gave it three cells, totaling about 4V (with an already used up button cell).
I was warned about overpowering the motor, but I have a transistor in place to prevent that.
Speaking of the transistor, that low-gain transistor I was using may not have been allowing enough current to the motor, so as suggested by my professor, I replaced it with a high-gain darlington transistor.

"Help! Assistance is required! I believe there is something wrong with me!"

Well, it's better than before, and I'm not getting anymore random shut-offs.
The problem, I suspect, is that the batteries have a current output limit that's just too low to power the controller AND the motor, even with 4V. When the batteries try powering the motor against the weight of the bot, it either just struggles, or reboots.
I'm sure my professor can tell me how to fix this, but I'm out of time, and that concludes my efforts to make this a functional toy.
At least it works fine when I hold it off the floor. I've got something to present!

"The fun levels will be at maximum efficiency!"

Thursday, January 24, 2013

The H-Bridge and Sounds!

Today, I had to move on to bi-directional motor control.
I was given a blank H-Bridge board and components, so I soldered it together. It has 4 diodes, transistors, and resistors.

 This is an H-Bridge.
H-bridges have 4 switches, and depending on how you attach the first two leads (transistor switches) to power or ground, the motor it controls will move either forward, backward, or stop.
Here's a video of me pulling pin 1 from ground to power, and pin 2 from power to ground.

You can hear my brother being completely unprofessional and ruining my video.

Finally, I move on to a sound device.  I've got to use it to create a short melody. Hopefully I can add sound effects to my hacked toy.
The piezo sounder is great for making annoying retro beep noises.

Our microcontrollers can easily control them with just two connections, and a program statement of "tone([pin#], [frequency], [tone duration]);"

Obviously, I've got a program that runs this statement several times with specified notes and durations. 

Next, I use a light sensor to control its pitch, kinda like a theremin. Simple analog input for an analog pitch output.

Yep, that's pretty freaking annoying.

Saturday, January 19, 2013

Using the Microcontroller

The first thing to do this time was to start hacking my robotic toy. 


I got a Hexbug.

I tore out its guts and went straight for the motor leads, soldered them to appropriate gauge wires, then attached it to my breadboard.
My motor requires very small voltage, so I used three diodes in series to reduce the 6 volts I was getting from my power supply.

I removed its wheels. Haha.

Next, I needed to learn how to use transistors to control the large voltage of the power supply with the small voltage of the microcontroller. Transistors can be chained in a "Darlington arrangement" to effectively create a more sensitive transistor that controls a larger current. 
I managed to use transistors and the microcontroller to light up a holiday light, which requires much more current than an LED.

 The two black components are transistors in a Darlington arrangement.

The controller activates the transistor's "base" which allows the larger current through the transistor.
The arrangement I created can be replaced with a specialized transistor component that houses the same arrangement.

 Ignore the green wire and small transistor.  :(


Then I switch out the light for my Hexbug. I can control the speed of its motor by varying the voltage it receives. The Launchpad allows me to output an analog voltage (0% - 100%) to the resistor, which in turn, sends about the same relative voltage to the motor.

My program cycled between slow and... extra slow.


Thursday, January 17, 2013

Microcontrollers!

Today, I get familiar with the soon-to-be brain of my hacked robots. I will program this MSP430 microcontroller to output or input whatever I want!
The next task was to use one pin on the controller for input, and one for output. The input pin registers "on" when I close its circuit with a push button, and the output pin lights up an LED.

I give you a brief look at my code here too, but I won't really go into that.

Then I ran a program that lights up multiple LEDs in sequence. My code designates the timing and order that they light up in.
The program's "setup" method begins the program with one light pulsing on and off at an increasing rate, then the main "loop" method runs several LED pulsing commands in sequence to create a scrolling effect like the car KITT from Nightrider.
I reset the program with the reset button on the controller. 

Also, there's close up of the microcontroller.

Last was to use the Light Dependent Resistor (LDR) as analog input for the controller, and use that input to output 4 different states, as indicated by the LED pattern. I just cover the LDR with my fingers to vary its input.

Full ambient light - both light
high-medium light - LED1 lights
low-medium light - LED2 lights
low ambient light - none light

Wednesday, January 16, 2013

Electronic Switches

I got behind. Catching up with the rest of the class, I did some fun stuff with relays. Relays are switches powered electronically. They have an electromagnet inside that controls its switch gates, and that magnet gets its own circuit.

Hah, that jumper cable is huge.
The red and yellow lights alternate when I activate the magnet. I know, yellow is super dim.

Next was to have the relay oscillate, in other words, operate itself.
I basically routed the magnet's circuit through the switch gates it controls.

It just buzzes because the relay is switching itself on and off ultra-fast. It's so fast, the lights just look dimmer.

This will destroy the relay pretty quickly though. 
What do? ...Capacitor!
A capacitor is basically a super-fast rechargeable battery. The capacitor will soak up the current and release it, over and over, effectively slowing down the alternation of the relay.

The capacitor I used was smaller capacity than what was recommended, so the lights alternate faster than everyone else's. *shrug*

Next is transistors. Finally!
-The heart and soul of electronics.
...OK, maybe not the soul. But still pretty freakin' important!
They have just three nodes: the collector, emitter, and base. The first two relay a current, and the base controls the current. It's similar to a relay switch, except faster, simpler, and cheaper.
Here I control an LED with a transistor that I actuate by completing a circuit with my finger.

The great thing about them is that a tiny current can control the flow of a large one.

Oh, and finally I built a robot from those parts I showed earlier. It's a simple "Boxbot," and it's currently just an r/c toy.

OK, I did not want this video in vertical, but that controller is HUGE, and I was my own camera man. Deal with it.


Thursday, January 10, 2013

Potentiometers and Switches

Today I played with more electronics and inventoried my parts for robot building.

I learned that components require optimal current to keep their lifespan healthy. For instance, the LED runs best at about 0.02 amps, so with a 5V power adapter, it should have 5/0.02 (Ohm's law) resistance in its circuit. Too much current will damage the component, reducing its lifespan.

 This is a photoresistor - it varies its resistance based on how much light it detects.

 This circuit has 250 ohms of resistors, resulting in less current than is optimal for the LED.

The potentiometer is simply a resistor with a knob that varies its resistance. If you couple a potentiometer with an optimal resistor, you emulate a house light fixture with an analogue control. The optimal resistor prevents the resistance from ever going too low!

A neat double switch circuit that emulates your average hall light switches. Flip either switch, and the light will toggle!


Soon I will build a simple robot from my newly acquired VEX parts.

 Here's a shot of my new parts (that got used last season and had ruined inventory)!
I had to re-inventory them and discard all the extra parts.


Using a breadboard and multimeter

For the first item on our agenda, I was given the opportunity to play with a breadboard using resistors, LEDs, and my custom power supply. LEDs light when completing the circuit, and resistors add resistance to the circuit, restricting current provided by the power source. The greater the current received by the LED, the brighter it lights.

 A breadboard circuit with two resistors, three LEDs, and power.

Next was to test batteries using a multimeter. The "1.5 V" batteries measure higher than 1.5 V on the multimeter when they're new, and older ones register much lower voltages.

 The two multimeter nodes held above a battery.

Just as previously understood, our power outlets provide a ~120V output.
Multimeter testing the alternating current voltage of the outlet.

The last item was to compare the voltage output of an old unregulated power adapter with a modern regulated adapter. The regulated adapter automatically adjusts the output based on how much resistance is in the circuit, so the voltage is consistent. We found that this was not the case for the unregulated adapter, as putting the wrong amount of resistance in its circuit would result in burning out the resistor.
Fortunately for me, I've got a modern power adapter.

Wednesday, January 9, 2013

Building and Testing a 5V power supply

This was my first project in ELEC10. The primary objective was simply to get some practice with basic electronics tools and procedures.

The first task was quite basic, just soldering components into a breadboard. The trick was to heat the copper pads sufficiently before applying the solder, so it would flow down and stick to the pad.
Small bread board with transistors and capacitors soldered into random locations. 

The next task was tricky because it required soldering two loose wires together without twisting.
Six separate wires were stripped and soldered together into a circle with heat shrink to cover the metal.

The last task was even more difficult, but I have not yet finished it. I stripped the surface insulation (black) from the two wires inside and then stripped a small length from the wires. The black insulation was really tough to sever without damaging the wires.
Next, I will solder the two wires to their respective leads pictured below, then shrink wrap one lead so the two don't make contact.
 The power supply with the output end cut off. The two wires are to be soldered to the leads, and the black shrink wrap slid down over the white wire's solder.