Archive for November 2014


We have all used breadboards, or those who plan to jump into the fascinating world of electronics need to know about them. This article focuses on introducing breadboards inside out, to help people use them and prototype their innovations faster. We would walk through Bread board concepts, connecting various chips & devices, efficient prototyping and documenting the designs.

We would like to detail about our tryst in making the DS1307 RTC to work on the Raspberry Pi. This exercise helped us to find out more in details of the I2C peripheral on Raspberry Pi and how we can make use of it. Going along the way we changed the buses and the way we approached our problem but again it was best to share that so that you are benefited and don't land up into the same problem that we faced.
We started with as Raspberry Pi B Model and a Tiny RTC module. This module contained both DS1307 RTC and an I2C EEPROM AT24C32 ( 4096 x 8bits = 32kbits ). Here is a picture of the module:
We know that the Raspberry Pi B model has two I2C peripherals I2C0 and I2C1 the details of this has been quite well documented in the Elinux Page for BCM2835_GPIO.
The I2C1 is available on P1 connector(26pin) and I2C0 is available on P5 connector(8pin).
We generally use the 26pin connector for lots of other purposes involving expansion of the RPi functionality. So we decided that we would use the I2C0 for the RTC interfacing. This was a wrong turn and we learn a lot from it.

Here are the steps that we took to configure the I2C or rather enable I2C functions on the Raspberry Pi:

[1] Update Raspbian on RPi:

We do this all the time before we start. First update the RPi raspbian and the boot loader. Here are the three simple commands.

[2] Load the Utilities needed:

We need to setup some useful packages that would help us to debug and install other programs from our favorite Github. There are basically two things - Packages needed to debug I2C and Packages needed to compile programs downloaded from Github.
sudo apt-get install -y i2c-tools git-core python-smbus \
build-essential gcc

[3] Configuring the I2C on Raspbian:

For this there are several steps that need to be followed =

  • Edit the /etc/modprobe.d/raspi-blacklist.conf file:
    sudo nano /etc/modprobe.d/raspi-blacklist.conf
  • Find the Line containing blacklist i2c-bcm2708 add a '#' in front of this line. to make it:
    #blacklist i2c-bcm2708
    Then press 'Ctrl + x' and then enter 'y'  to save modified the file.
    This helps to enable the I2C peripheral on the Chip.
  • Edit the /etc/modules file:
    sudo nano /etc/modules
  • Add the Line i2c-dev at the end of the file.
    Then press 'Ctrl + x' and then enter 'y'  to save modified the file.
    This helps to load the I2C module kernel driver at boot time.
  • Edit the /boot/cmdline.txt file:
    sudo nano /boot/cmdline.txt
  • At the end of the line add  bcm2708.vc_i2c_override=1 . Make sure that you don't add a new line, it should be added to first line end only with a single space.
    This helps to Enable I2C0 bus with override.
  • Now we need the access permissions for the I2C:
    sudo adduser pi i2c
    This would help to access the i2c bus even in normal user mode.
  • Finally Reboot the RPi 'sudo reboot'

[4]Checking the I2C busses:

We have already installed the 'i2c-tools' package in previous steps, this would help us to detect devices on the I2C bus. First check if you have the I2C buses available after reboot:
ls /dev/i2c*
This should display some thing like:
/dev/i2c-0  /dev/i2c-1
These are the two I2C Buses we would need.
Now to check if there are any devices on the I2C bus we issue the detect command:
i2cdetect -y 0
This displays the status of the I2C Bus 0:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
This means that there are no devices on the I2C bus 0.

We were surprised as we had already connected the I2C0 to the RTC based Elinux Page:
P5 Connector Courtesy: elinux.org
P5 Header Pinout (seen from the back of the board), top row:
Pin NumberPin Name Rev2Hardware NotesAlt 0 FunctionOther Alternative Functions
P5-015V0Supply through input poly fuse
P5-03GPIO28I2C0_SDAALT2 = PCM_CLK
P5-05GPIO30ALT2 = PCM_DIN ALT3 = UART0_CTS ALT5 = UART1_CTS
P5-07GND
P5 Header Pinout (seen from the back of the board), bottom row:
Pin NumberPin Name Rev2Hardware NotesAlt 0 FunctionOther Alternative Functions
P5-023.3 V50 mA max (combined with P1)
P5-04GPIO29I2C0_SCLALT2 = PCM_FS
P5-06GPIO31ALT2 = PCM_DOUT ALT3 = UART0_RTS ALT5 = UART1_RTS
P5-08GND

This was the place we referred.  On careful monitoring using our logic analyzer we saw no waveform on the I2C0 lines from P5. We were surprised.
Then we noticed something important !
I2C 0 works on the P5 only if the ALT0 is enabled on GPIO28 and GPIO29
This was the catch ;-) . We knew about the useful package called WiringPi which gives the capability to configure and access the GPIO via C programs. Lets look at our next steps to installing the WiringPi and then configuring the GPIO.

[5] Installing WiringPi on Raspbian for RPi:

We have already installed the git package so lets start by now checking out the repository and then compiling.
git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
The last command ensure that we have the updated version of the package.
Now give the following command to build and install the WiringPi package.
./build
We are done installing the libs for the Wiring Pi. This is an very useful utility supplied with this package called as 'gpio' this helps to easily visualize the GPIO status.
Lets try it out:
gpio readall
We get:
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |  28 |  17 | GPIO.17 |   IN | 1 | 51 || 52 | 0 | IN   | GPIO.18 | 18  | 29  |
 |  30 |  19 | GPIO.19 |   IN | 0 | 53 || 54 | 0 | IN   | GPIO.20 | 20  | 31  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
This shows that the GPIO28(Wiring Pi 17) and GPIO29(Wiring Pi 18) are Inputs and not the ALT0 function that we needed. Now we need to do a small trial to see if we can get the I2C0 to work.
Enter the following commands:
gpio -g mode 28 alt0
gpio -g mode 29 alt0
gpio readall
We get:
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |  28 |  17 | GPIO.17 | ALT0 | 1 | 51 || 52 | 0 | ALT0 | GPIO.18 | 18  | 29  |
 |  30 |  19 | GPIO.19 |   IN | 0 | 53 || 54 | 0 | IN   | GPIO.20 | 20  | 31  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
That's it we have both the GPIO pins now in the ALT0 function mode.
Now lets try our I2C detect command on the I2C 0 bus.
i2cdetect -y 0
We got:
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
We have detected two devices now !

For AT24C32 the 7-bit I2C address 0b1010(A2)(A1)(A0) - In current board A2, A1, A0 = GND so it would become 0b01010000 = 0x50
 For DS1307 the 7-bit I2C address 0b1101000 = 0x68
Hence we have the setup ready.

However we found a note in the following forum thread:
http://www.raspberrypi.org/forums/viewtopic.php?f=100&t=83897
This came out when we were searching for the I2C0 bus problem.
I2C0 is used by Videocore and by default is enabled on GPIO28/29. It will be the VC bootloader that temporarily swaps the alt settings around and probes the GPIO0/GPIO1 I2C0 bus for the EEPROM prior to booting the ARM. There should be no need to probe the EEPROM from Linux in a fully functioning system, but for development or test purposes the hack in the linked thread can let you talk to the EEPROM in Linux.
This means that we can't use the I2C0 at boot time and hence RTC functionality would not be suited on this.
Hence we need to move back to the I2C1 which is available on P1. Lets now look at the final modifications to connect the RTC module to the Raspberry Pi.

[6] Connecting RTC to Raspberry Pi:

We actually soldered connections such that RTC module acts like an extension for the I2C for future.
Power Connections for RTC
I2C Connections at bottom
This way we can add additional modules on the P1 connector top and still have the RTC functionality.
Here is the complete setup:
Raspberry Pi B connected to Tiny RTC module
This completes our hardware setup. Next comes the software part to be configured so that we can have the RTC working.

[7] Configuring the RTC DS1307 on Raspberry Pi in Raspbian:

For this we heavily depend on Adafrut Raspberry Pi RTC Tutorial, we are just quoting the same for your convenience.
We need to type in the following commands:
sudo modprobe rtc-ds1307
sudo -i
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
exit
sudo hwclock -w
Lets describe whats going on here.
The first command loads the rtc kernel driver for DS1307. The second command enters into a root shell where we need to enter the third command. The third command connects the RTC to the System I2C 1 Bus.
The fourth command exits the root shell we entered in the second commands. The last command sets the DS1307 internal time. It is important to note that one must boot the Raspberry Pi connected to the internet to get the correct system time before you set the DS1307 internal time.
In order to verify that the time in the RTC DS1307 set properly give the following command:
sudo hwclock -r
This command directly reads the RTC internal time. If everything goes well then this should show the same date and time as that of the 'date' command.

Now we need to modify some files to make this change permanent. Means that we wish to use the RTC time always. Here are the steps to do that:

  • Edit the file /etc/modules to enable the DS1307 driver to load at boot time like we did for the I2C:
    sudo nano /etc/modules
  • Add the following line at the end of this file:
    rtc-ds1307
    Then press 'Ctrl + x' and then enter 'y'  to save modified the file.
  • Edit the file /etc/rc.local to enable the attachment of the RTC to I2C and to set the system time with the RTC time. This ensure that even when the Internet is not connected the RTC time is set as the default system time.
    sudo nano /etc/rc.local
  • Add the following lines before the 'exit 0' at the end of this file:
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
    sudo hwclock -s
  • Next Reboot the Raspberry Pi using 'sudo reboot'
Now the Raspberry Pi would always show correct time no matter if its online or not.

Thats all, hope that you like this post. Let us know your feedback and suggestion to help improve this post.

Welcome to Boseji's Blog

Popular Post

Blogger templates

Welcome

Creation by Electronics Hobbyists from Bharat(India) to encourage & help people. This blog hosts the numerous innovations, discoveries and projects we provide.
Our Research Wing Adhar Labs
Contact Form | Email Address
We Support Open Source Hardware Definition v1.0
Creative Commons License
All works on this blog are licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at forbharat.wordpress.com and m8051.blogspot.com.

Permissions beyond the scope of this license may be available at http://forbharat.wordpress.com/permissions/.
Thank you for all your support.

© A.D.H.A.R Labs Research 2008-2013 . Powered by Blogger.

Follow by Email

Followers

PCB Unit Converter

mils
mm

- Copyright © Electronics for Bharat -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -