Friday 22 November 2013

From output to input...

With my very first hardware experiment completed successfully, I felt compelled to continue... for science!

So having 'mastered' the art of software-to-hardware output, the next logical step for me was hardware-to-software input. This would involve some sort of switch or button, presumably making the PC do something when I pressed it. After many hours of study through ancient tomes of historical text by candlelight (read that last sentence as 'a quick bout of google-fu'), I felt ready to start on my next project - the 'click a button and make something happen' experiment.
The circuit.

As you can see from my awesome graphic skills there, it's quite a simple circuit again. Basically, I'm connecting Pin11 of the RPi (which is GPIO17) to Pin1 (3.3V) through a large resistor. By large I mean high resistance, not physically big. I'll explain why I need the resistor in a minute, but back to the circuit...

You can also see a normally open, momentary switch which leads to Pin6 (Ground) on the RPi. So when the switch is open, it's getting a small voltage to Pin11 from Pin1. But if I push the switch, the circuit is closed and Pin11 is effectively shorted to Pin6 - the Ground. These differing states should be all we need to let the Python program know whether or not the switch has been pressed.

Now back to that resistor. If you can imagine the circuit without that resistor in there for a minute, it's obvious that if I was to press the switch, I would be shorting 3.3V directly to Ground. A dead short. Not really a clever thing to do. So the large (10K) resistor ensures that only the tiniest amount of current is drawn when the switch is pressed, thus protecting the RPi. I hope.

The code.




So the code starts with the same lines as last time, basically using existing libraries coded by some clever person somewhere, sometime. Once again, thanks, whoever you are.

I have commented the code to try and explain what each line does. The reason I needed the variable called "last_state" is because the little RPi is so quick that without it, a single button press would be read many times, and that isn't what I was trying to do. Er, how can I explain that?... I wanted to pick up one click of the button, no matter how long I kept my finger on it. Without that variable being used as a flag, holding my finger on the button would cause the program to read it as many, many clicks instead of one long one.

After a bit of tweaking and a cup of tea which seemed to go cold quicker than usual, here is a picture of the resulting output...



Here, have a video, it might make more sense...



  So there we have it. Experiment number 2 successfully completed :)

Just one more thing, the technically astute viewer might notice that I have no mouse, keyboard, or video output attached to the RPi now - just power and Ethernet. Well to be honest, I got fed up of having two mice, two keyboards, and a third monitor on my desk (I use two monitors usually), so I am now connecting to the RPi over my local network using SSH. It's like a remote desktop kind of thing, very clever and extremely handy. It also performs very, very well on this tiny little device.

Anyway, I digress. I'm cooking up ideas for experiment number 3 which will no doubt be the marriage of the two experiments completed so far. Probably, a physical input to the RPi, which is read by the Python program, processed in some way, then output physically somehow via the RPi. 

It might be time to break out the LED's again, perhaps a different colour this time as I'm feeling adventurous!

Stay tuned for the next epic adventure. Or something.

Monday 18 November 2013

20 months later, a new project..

So er, it's been 20 months since my last blog post. Where the hell did that go? A few LAN parties, many new game releases, a few books read etc, but more recently a new project!..

You may or may not have heard about something called the Raspberry Pi . A bit of a daft name, yes. But the Raspberry Pi, or RPi as it's known, is a complete computer on a credit card sized circuit board. And it's cheap. Like £30 cheap. Not bad for a fully functional computer!

This little device is very capable, given it's price. People are using it as a media centre running a version of XBMC (it can render 1080p video over HDMI) , a lot of people are using it as an aid to learning to code, some for gaming - this thing even runs Quake 3 Arena!, and some are using it to tinker around with "other" things. And I fall into that group.

Having delved into various forms of coding over the years, from Assembler(Yuck!) to BASIC to C++ to DarkBASIC to JAVA (omg NO THANKS!) I believe the time has come for me to have a play with the hardware side of things. The RPi doesn't come with a pre-installed operating system as such, but is based on LINUX, and a number of tools are freely available for it. One language that looks easy enough to learn is Python , and this apparently works very well with the RPi. So I headed over to Codecademy and worked my way through a few basic Python lessons. Sure enough it is quite simple. At least the first few lessons are, I haven't got very far yet :)

As I said, I want to learn a little about hardware, and more specifically how it interacts and interfaces with software. The fact that a program can run within a computer and directly control real-world tangible devices fascinates me. So armed with my RPi and a few electronic bits and bobs gleaned from ebay, I set my sights on intergalactic robotic domination. But I thought I'd better start off small, so I present to you my "Python-based RPi controlled Flashing LED" project..

The bits.

Here we have the RPi itself, a 'breadboard' (for easy circuit prototyping), a few random electronic components, and a selection of 'male to female' jumper leads. As you can see, the RPi has an SD Card plugged in one end (this holds the software). Also connected here are two USB devices (mouse and keyboard), a HDMI device (monitor) and the Ethernet for Internet/Network access. If you enlarge the image (by clicking on it) you will see a row of pins to the bottom-right of the board. These are the GPIO pins (General Purpose Input Output) and these are what we use to interface to external hardware.

The circuit.

This is about as basic as an electronic circuit will get.The brown jumper is linking a GPIO pin on the RPi to the Positive leg (otherwise known as the Anode) on the LED. I have then connected a resistor to the Negative leg (Cathode) in series (more about electronic circuits in future posts unless I burn the house down first) and then close the circuit back to the RPi by connecting the black jumper to the Ground GPIO pin. The resistor is there to protect the LED because the RPi logic runs at 3.3Volts, and the LED is only 1.7V, but that's a bit technical and not for this particular post :)

So that's the hardware wired up, now onto the software. Using the free Python editor that comes with the RPi software suite, I created this program...

Apologies for the picture, I haven't learned how to do screen grabs on the RPi yet.

I've commented every line to explain what the program is doing. The part before the 'While' loop is a bit geeky, but basically I have imported a pre-defined library so that I have direct access to the GPIO pins on the board. Thank you to whoever wrote that library :)

So after some administration rights issues which I won't bore you with here, I ran the program and here's what happened...



Not exactly mind blowing I agree, but it's the first experiment of what I hope to be many. In future blogs I intend to include circuit diagrams and better screen captures. Unless something else comes along in the meantime, I'm easily distracte...oooooh, Top Gear is on...