Exploring Edison - I2C Bus
Written by Harry Fairhead   
Monday, 23 November 2015
Article Index
Exploring Edison - I2C Bus
I2C MRAA Functions
Waiting for data

Slow Read

This raises for the first time the question of how we cope with the speed that a slave can or cannot respond to a request for data. 

There are two broad approaches to waiting for data on the I2C bus.

The first is simply to request the data and then perform reads in a polling loop. If the device isn't ready with the data then it sends a data frame with a NAK bit set. The mraa read functions return a zero if it fails or the data if it doesn't. Of course the polling loop doesn't have to be "tight". The response time is often long enough to do other things and you can use the I2C bus to work with other slave devices while the one you activated gets on with trying to get you the data you requested. All you have to do is to remember to read its data at some later time.

The second way is to allow the slave to hold the clock line low after the master has released it. In most cases the master will simply wait before moving on to the next frame while the clock line is held low. The Edison I2C bus implements this clock stretching protocol and it will wait until the slave releases the clock line before proceeding. This is very simple and it means you don't have to implement a polling loop but also notice that your program is frozen until the slave releases the clock line. 

Many devices implement both types of slow read protocol and you can use which ever suits your application. 

There is also the small matter of the speed of the I2C clock. In principle the clock can run at almost any speed but in practice this usually isn't the case. Most slave devices don't have strict clock rate that have to be used with them as they are designed as static devices synced to whatever SCL clock rate the master cares to use. If it turns out to be too fast then most will use clock stretching. In extreme cases it may be necessary to slow down or even speed up the master's clock.

There is an mraa function to do this: 

mraa_i2c_frequency (i2c,mode)

This works with the Edison if you are using the latest mraa library. You can set one of three modes:

• MRAA_I2C_STD Standard mode  100Kb/s

• MRAA_I2C_FAST Fast mode 400Kb/s

• MRAA_I2C_HIGH High-speed mode  3.4Mb/s


By default the Edison works in Fast mode and you can select standard but at the time of writing high doesn't seem to work. 

FSYS Linux I2C Commands

As with most Linux hardware, the necessary drivers represent the hardware as files. This is how it is with I2C and a collection of I2C tools that you can use from the command line are available. Sometimes these are useful, but in the case of the Edison they tend not to work as well as you might hope - and particularly so with I2C 6.

My advice is not to rely on the information you get back from the Linux tools because they often fail to detect devices that actually exist on the bus. 

The commands available are:

To scan the bus report devices connected:


To dump registers:


To read a device register:

To set a device register:


For example, to scan the buses installed you can use:

i2cdetect -l

This will list eight possible I2C buses on the Edison. Of course, for hardware reasons ,you can only use 2, I2C-1 and I2C-6.

To list all of the devices connected to a bus you can use:

i2cdetect -r 1

which scans bus 1. Notice this often results in a crash.


To dump all the registers in a particular device on I2C-1 use:

i2cdump 1 address w

To read a particular register on a particular device use: 

i2cget 1 address register w

Finally to set a particular register on a particular device use:

i2cset 1 address register data w

Some programmers like using these commands to check that slave devices are present and working. Personally I would prefer to write a C program simply because the results are more reliable.

If you want to know more about the Linux i2c tools consult the man pages. 

Where Next

So much for theory. In the next chapter we try things out with a simple temperature and humidity sensor - the HTU21D.



More Information

The starting point for finding out about all Intel's Internet of Things resources, including Edison, is the Intel IoT Developer Zone.


Exploring Edison



Now On Sale!

You can now buy a print edition of Exploring Intel Edison.
You can buy it from:

USA and World  Amazon.com
Canada              Amazon.ca
UK                      Amazon.co.uk
France                Amazon.fr
Germany            Amazon.de
Spain                  Amazon.es
Brazil                  Amazon.br
Italy                    Amazon.it
Japan                 Amazon.co.jp
Mexico               Amazon.com.mx 

Chapter List

  1. Meet Edison
    In this chapter we consider the Edison's pros and cons and get an overview of its structure and the ways in which you can make use of it. If you have ever wondered if you need an Edison or an Arduino or even a Raspberry Pi then this is the place to start. 

  2. First Contact
    When you are prototyping with the Edison you are going to need to use one of the two main breakout boards - the Arduino or the mini. This chapter explains how to set up the Edison for both configurations. 

  3. In C
    You can program the Edison in Python, JavaScript or C/C+ but there are big advantages in choosing C. It is fast, almost as easy as the other languages and gives you direct access to everything. It is worth the effort and in this chapter we show you how to set up the IDE and get coding. 

  4. Mraa GPIO
    Using the mraa library is the direct way to work with the GPIO lines and you have to master it. Output is easy but you do need to be aware of how long everything takes. Input is also easy but using it can be more difficult. You can use polling or the Edison interrupt system which might not work exactly as you would expect.

  5. Fast Memory Mapped I/O
    There is a faster way to work with GPIO lines - memory mapped I/O. Using this it is possible to generate pulses as short at 0.25 microsecond and read pulse widths of 5 microseconds. However getting things right can be tricky. We look at how to generate fast accurate pulses of a given width and how to measure pulse widths.

  6. Near Realtime Linux 
    You need to be aware how running your programs under a non-realtime operating system like Yocto Linux effects timings and how accurately you can create pulse trains and react to the outside world. In this chapter we look the realtime facilities in every version of Linux. 

  7. Sophisticated GPIO - Pulse Width Modulation 
    Using the PWM mode of the GPIO lines is often the best way of solving control problems. PWM means you can dim an LED or position a servo and all using mraa. 

  8. Sophisticated GPIO -  I2C 
    I2C is a simple communications bus that allows you to connect any of a very large range of sensors. 

  9. I2C - Measuring Temperature  
    After looking at the theory of using I2C here is a complete case study using the SparkFun HTU21D hardware and software. 
  10. Life At 1.8V
    How to convert a 1.8V input or output to work with 5V or 3.3V including how to deal with bidirectional pull-up buses.

  11. Using the DHT11/22 Temperature Humidity Sensor at 1.8V 
    In this chapter we make use of all of the ideas introduced in earlier chapters to create a raw interface with the low cost DHT11/22 temperature and humidity sensor. It is an exercise in interfacing two logic families and implementing a protocol directly in C. 

  12. The DS18B20 1-Wire Temperature 
    The Edison doesn't have built in support for the Maxim 1-Wire bus and this means you can't use the very popular DS18B20 temperature sensor. However with a little careful planning you can and you can do it from user rather than kernel space. 

  13. Using the SPI Bus 
    The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so, if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. 

  14. SPI in Practice The MCP3008 AtoD 
    The SPI bus can be difficult to make work at first, but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12-bit AtoD using the MCP3008.

  15. Beyond mraa - Controlling the features mraa doesn't. 
    There is a Linux-based approach to working with GPIO lines and serial buses that is worth knowing about because it provides an alternative to using the mraa library. Sometimes you need this because you are working in a language for which mraa isn't available. It also lets you access features that mraa doesn't make available. 




To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin.


raspberry pi books



or email your comment to: comments@i-programmer.info






Last Updated ( Monday, 23 November 2015 )