The second half of this exercise is to read values of the pins. The kit came with a button, and I did my best to connect the button properly. There are four leads which stick out of the left and right sides, and when arranged in this way it straddles the middle of the breadboard perfectly. The two leads on the top are always connected, the two leads on the bottom are always connected, and pushing the button connects them all together.
The top left lead was connected to the ground pin using a 10k resistor that also came with the kit. I actually pulled out a different resistor at first, and it took a quick Google search to understand the colored band system for labeling them. (The other one was only a 220 resistor.) The top right was connected to GPIO23. The bottom left was connected to the 3.3V pin.
Setting up the pin to read input followed a similar form as making the pin an output.
- echo 23 > /sys/class/gpio/export
- cd /sys/class/gpio/gpio23
- echo in > direction: This is what sets the pin to an input
- cat value: This gives the value of the pin. It will either be 1 (high) or 0 (low).
Unfortunately, I was getting erratic results. Sometimes it was a 0, sometimes it was a 1, and it didn't really seem to matter whether the button was pressed or not. Both books warned that this could happen. It has to do with the pin being a "floating pin" when it's not connected properly. I'm still not quite sure about the physics behind this, but it looks like it has to do with small random currents creating a large enough voltage difference because of high resistance. Since V = IR, large R causes large voltages even for small currents. The resolution to the problem is to stick the 10k resistor between the GPIO and the ground, which sets the "normal" voltage to zero.
But everything was already connected, and I tried double checking the connections with no luck. So I went out and bought a multimeter so that I could test the connections directly. The multimeter is a Southwire 10030S and cost exactly $27 from Lowes after tax. This brings the total cost of this experiment to $123.38.
After getting back home and poking around a while, I found that the button wasn't making good contact, so I pulled it out and put it back in. I don't know why this didn't work before, but this time it did. Now the reading was 0 when the button was up and 1 when the button was down.
My next task will be to use Python to read the pins. It's probably not going to happen for at least a week, as next week is very busy.
No comments:
Post a Comment