Introduction: Max the Spider - Powered by LEGO and PIC Microcontroller

Picture of Max the Spider - Powered by LEGO and PIC Microcontroller

So Max the spider visited us in the lab today, and wanted to get into the halloween spirit. "I'd like to drop down on people in the elevator," he smirked, in a way only a spider could.

"I've got just the thing," I winked, and put a little something together. You're looking at a clear Lego motor (comes with its own gearbox), mounted on a Lego platform. The platform is not going to win any design awards, but it is stable and holds the motor, circuit board and 3 AAA batteries pretty well.

I tied a thread to max, and hoisted him on the plastic winch. When I press a button on any regular remote control, he descends on hapless victims with relish. Tee hee.

Video: nwanua.aniomagic.com/max-the-spider.mov

Step 1: Circuit Board

Picture of Circuit Board

The circuit board contains:

- PIC 12f675 microcontroller
- PNA4602M infrared detector
- 2 IRL9530N power MOSFETs (P-channel)
- 2 IRL520N power MOSFETs (N-channel)
- 100uF capacitor
- five 1n914 diodes
- 3 AAA batteries

The whole thing is probably over-designed, and the transistors are overkill, but this is what I could grab and put together in 30 minutes. If the inputs to the H-bridge (four transistors) are different, your motor will turn (eg. 0,1 to turn CW, and 1,0 to turn CCW). If the inputs are the same ( 0,0 or 1,1) the motor is shorted and it stops very quickly. Don't leave out the diodes, as they prevent the motor from spiking energy back into the transistors.

The extra diode and capacitor to the left of the IR sensor help smooth out small voltage ripples caused by the motor.

Step 2: The Code

-- file: spider.jal
-- PIC: 16F675
-- purpose: drop and raise spider when infrared hits the sensor
-- Do what you want with the code.

include f675_4i
include jdelay

var bit M1 is pin_a5
pin_a5_direction = output

var bit M2 is pin_a4
pin_a4_direction = output

var bit IR is pin_a2
pin_a2_direction = input

var volatile byte gpio at 0x05
var volatile byte cmcon at 0x19
var volatile byte wpu at 0x95
var volatile byte ansel at 0x9F

cmcon = 0b0001_0111 -- comparator off
gpio = 0b0000_0000 -- initialize i/o pins low

-- please consult the datasheet to find out more about register banking
asm bsf status_rp0 -- bank 1
ansel = 0b0000_0000 -- turn off A/D, make all pins digital I/O
OPTION = 0b0000_0000 -- bit 7:allow pullups globally
wpu = 0b0001_0100 -- enable individual pullups
asm bcf status_rp0 -- return to bank 0

-- normally, the IR sensor is high if it doesn't detect a 38kHz IR beam
-- if it does, it takes the line low
-- the code is not robust, and relies on the sensor to reject spurious signals
forever loop
M1 = high
M2 = high

while (IR) loop end loop
delay_1ms (1)

if (! IR) then
delay_1ms (1)

M2 = low
delay_100ms (7)

M2 = high
delay_100ms (1)

M1 = low
delay_100ms (2)
M1 = high
delay_100ms (1)

delay_100ms (13)

for 3 loop
M1 = low
delay_10ms (12)
M1 = high
delay_10ms (8)
end loop

delay_100ms (8)
delay_100ms (8)

for 6 loop
M1 = low
delay_10ms (6)
M1 = high
delay_10ms (8)
end loop
end if

while ( ! IR) loop end loop
delay_1s (3);

end loop

Comments

botronics (author)2011-10-07

I like your spider. My wife (a teacher) wants me to make one that is sound activated (in reverse) to put in the classroom. If the class is quiet, the spider will come down. As soon as it gets too noisy, it goes back up, just like a real spider does. This way, the students will want to be quiet in the classroom so the spider will descend.