Monthly Archives: Květen 2012

Raspberry Pi (2) – Experiments with GPIO

RPi is working, so what to do now? Some hardware hacking! I want my RPi to detect when somebody came into room and to do something (for example fire an flame-thrower on the intruder). So let’s try it.

Dsc_2045

Some theory

Raspberry Pi is equipped with expansion connector – 2×13 pins with standard 1/10″ spacing. Connector is described on RPi wiki.

There is one I2C and one SPI bus on connector – it will be tested later. 5V, 3.3V and GND connection. And an 8 GPIO ports.

It is lower nuber of GPIO than on any Arduino, I know. There is no analog input/output, also. But I don’t like writing code in low-level languages like on Arduino, and I want to use user-friendly and powerfull platform. RPi is much better for me.

GPIO ports on RPi are 3.3 volts and are not 5 volt compatible. If you connect them to 5V signal, your RPi will be dead instantly. So be carefull.

All GPIO ports are available as files under /sys filesystem. But you have to be root for using them – and this is bad. I don’t want running my code under root account, it is a bad behaviour.

There is a solution. People from quick2wire.com released some software solving this trouble. You can „open“ GPIO pin by using

gpio-admin export 22

then you can read from port

cat /sys/devices/virtual/gpio/gpio22/value

or even write to the port

echo out > /sys/devices/virtual/gpio/gpio22/direction
echo 1 > /sys/devices/virtual/gpio/gpio22/value

directly from the shell.

 

Driving a LED from RPi

It’s easy. I’ve bought a LED with 2 mA driving current – so low so I can connect it directly to RPi. Connected it with a 680 ohm resistor (i fact two serial connected 330 ohm resistor) between GPIO port 24 and a GND. Then I tried:

gpio-admin export 24
echo out > /sys/devices/virtual/gpio/gpio24/direction
echo 1 > /sys/devices/virtual/gpio/gpio24/value

and LED starts shining.

 

Using a PIR detector with RPi

PIR stands for „passive infrared“. It’s a basic security/surveillance device – it can detect movement in indoor area by detecting changes in infrared radiation

For a less than 6 USD you can buy a cheap PIR detector from china e-shop with free shipping to EU. Delivery time was about 10 days – better than some czech e-shops…

It is powered by 5 V (available on RPi expansion connector). Output signal is 0 V when nothing happens, and 3.3 V when movement is detected – so it is compatible with RPi GPIO pin with no conversion need.

So I connected it to 5 V, GND and GPIO 21. And wrote some shell script, which:

  • waits for a „0“ signal on GPIO 21 – at start, we will wait for „iddle“ status, no movement detected
  • executes an infinite loop:
    • waits for a „1“ signal on GPIO 21
    • display time and a message on console – „VETRELEC“ (means „intruder“ in czech lng)
    • sends „1“ to GPIO 24 – so LED is switched ON and shining
    • waits for end of a alarm – a „0“ signal on GPIO 21
    • display time and a message on console – „Konec poplachu“ (means „end of alarm“)
    • sends „0“ to GPIO 24 – so LED is switched off

Started the script and … wait for it … it works. When somebody entered the room, message is printed and LED is switched ON. No movement in room -> LED is black. I’m so happy!

 

Dsc_2046

 

Next time to try: Connect to the Lego Mindstorms NXT (robotics set from Lego) with Raspberry Pi.

 

Links:

Raspberry Pi – first steps and experiences

Some experiences I’ve gained during my first Raspberry Pi‚s tests:

 

1) Fedora sucks.

Really. Fedora Remix, the one distro recommeded by Pi team, is not the best option – at least for me. Just to be sure – I’m talking about version 14 of that.

There is no URL of image on wiki page (nor anywhere I looked at). You have to download installer which will do everything for you… if it can found your card reader. It didn’t on my Windows XP laptop. And there is no way to override automatic detection.

I downloaded VmWare image with Fedora linux and tried the installation from this environment. It worked like a charm, of course.

After first Pi’s boot I’ve found that author of the Fedora Remix distro set password expiration on default accounts. This is a bullshit. Raspberry Pi has no real time clock chip onboard, so if you boot it up with no internet connection, date is set to 1970/1/1. If you connect it, date is set by NTP. So you boot for the first time, with no internet connetion. Password is expired, you have to change it. Then you boot with internet. Password is expired, you have to change it.  You want to try connection with your plasma TV – so you move to another room, connect it to TV (and not to internet) … and shit, password is expired, you have to change it. And so on.

And at last, but not least, I was unable to start X Windows on Fedora Remix. Some error messages on console was all I’ve got.

One positive point for Fedora – it resize partition on SD card to use all the space available. Automatically, during the first boot. 

 

2) Debian is much better – but some tunning is required

Second time I’ve tried Debian „squeeze“ (version 2012-04-19). I copied image to the SD card in virtual Ubuntu box in VmWare with standard „dd“ command … and Pi started up without problem.

This distro’s author didn’t forgot about missing RTC chip in Pi. I logged on with no password expiration message. Default configuration is smart – it detect that date is 1970/1/1 and displays a message like „Time/date seems invalid, you can set it via ‚date …‘ command“. Thumbs up – this is what I call „user friendly“.

X Windows works like a charm. LXDE is simple, but everything I need is there.

British keyboard and british time zone is set by default. I’ve found this article describing how to configure it to something more usefull. There are handful of other tips and tricks here – you should read it.

Next step I did was firmware update. I’ve installed firmware updater from Heexeh. Just did the things mentioned in readme and all proceed OK.

I set memory split to 224 MB for linux / 32 MB for video. 

There are little problems with SD card during the boot after the firmware update. It is mentioned on forum somewhere – and I meet it also. Boot process works for about two seconds, then plenty of error messages are displayed – error during reading SD status register and other similar. Maybe my SD card is too fast for Pi. But after some time (from 30 seconds to three minutes) the boot process resumes and everything is working good.

Debian image is about 2 GB in size. If you have bigger SD card, the rest of SD card is left unused – maybe it is better to resize partition to eat the whole card size. There is no automatic partition resize like in Fedora – I miss this feature. So I had to boot Ubuntu image in  VmWare, insert SD card to reader and use the GParted tool. Partition resize is easy-as-a-breathing in GParted.

Some other tips for debian configuration are here.

 

3) XBMC is working good, too

I’ve tried XBMC within OpenELEC distro. I used the precooked image described in this forum post.

It works!  

  • FullHD 1080/24p video played on my 50″ TV with no problems
  • Subtitles in Czech language with both local encoding (Windows-1250) and UTF-8 – no problem. 
  • Streaming from network – no problem. 
  • AVI, MPEG and MKV containers works.

Good work, XBMC team.

I’ve found some videos which can’t be played – and XBMC seems to be somewhat confused after trying to play them, so I have to reset Pi for playing another video. Strange.

XBMC is not really fast on Pi. Video playback is good, because it is done on GPU, but XBMC environment – menu, configuration and so on – is really slow. I will not use Pi asi a media player device for my TV – but it is still impressive that such a small computer can play Full HD video.

Next step on the XBMC field: I will try to use DLNA server on my Nokia 701 phone for videostreaming.

 

Links: