Page 102 - Programming the Raspberry Pi Getting Started with Python
P. 102

a “slave” device and hence only listens on the SDA and SCL lines. Other I2C devices may act as a
               master device, and if they are 5V, there is a good chance this could damage your Pi. Therefore,
               before you connect any I2C device to your Raspberry Pi, make sure you understand what you are
               doing.
             We can now link the Cobbler to the Raspberry Pi using the ribbon cable supplied with the Cobbler.
          This should be done with the Raspberry Pi powered down. The cable will only fit one way into the
          Cobbler, but no such protection is provided on the Raspberry Pi. Therefore, make sure the red line on
          the cable is to the outside of the Raspberry Pi, as shown in Figure 10-1.
             Turn on the Raspberry Pi. If the usual LEDs do not light, turn it off immediately and check all the
          wiring.

          Software
          Everything  is  connected,  and  the  Raspberry  Pi  has  booted  up. However,  the  display  is  still  blank
          because we have not yet written any software to use it. We are going to start with a simple clock that
          just displays the Raspberry Pi’s system time. The Raspberry Pi does not have a real-time clock to tell
          it  the  time. However,  it  will  automatically  pick  up  the  time  from  a  network  time  server  if  it  is
          connected to the Internet.
             The  Raspberry  Pi  displays  the  time  in  the  bottom-right  corner  of  the  screen. If  the  Pi  is  not
          connected to the Internet, you can set the time manually using the following command:


             However, you will have to do this every time you reboot. Therefore, it is far better to have your
          Raspberry Pi connected to the Internet.
             If you are using the network time, you may find that the minutes are correct but that the hour is
          wrong. This probably means that your Raspberry Pi does now know which time zone it is in. This can
          be  fixed  by  using  the  following  command,  which  opens  up  a  window  where  you  can  select  your
          continent and then the city for the time zone you require:


             At  the  time  of  writing,  in  order  to  use  the  I2C  bus  that  the  display  uses,  the  Raspbian  Wheezy
          distribution requires that you issue a few commands to make the I2C bus accessible to the Python
          program we are going to write. It is likely that later releases of Raspbian (and other distributions) will
          have the port already configured so that the following commands are not necessary. However, for the
          moment, here is what you need to do:






          NOTE   You may find that you have to issue the last two of these commands each time you reboot the
               Raspberry Pi.
             So now that the Raspberry Pi knows the correct time and the I2C bus is available, we can write a
          Python program that sends the time to the display. To help simplify this process, I have produced a
          Python  library  module  specifically  for  this  kind  of  display. It  can  be  downloaded  from
          http://code.google.com/p/i2c7segment/downloads/list.

             As with other modules you have installed, you need to fetch the file, extract it into some convenient
          location (using tar -xzf), and then issue the following command to install it under Python 2:

             The  actual  clock  program  itself  is  contained  in  the  file  bundle  that  accompanies  this  book  (see
          www.raspberrypibook.com); it is called 10_01_clock.py and is listed here:
   97   98   99   100   101   102   103   104   105   106   107