Can Get I2c or Spi to Work on Raspberry Pi

Favorited Favorite 22

I2C on Pi

Configuration

The I2C peripheral is not turned on by default. There are two methods to adjust the settings only like the SPI. To enable it, practise the following.

Raspberry Pi Configuration via Desktop GUI

You tin can apply the Desktop GUI past heading to the Pi Start Carte > Preferences > Raspberry Pi Configuration.

Raspberry Pi Configuration via GUI

Click on paradigm for a closer view.

A window will popular up with unlike tabs to accommodate settings. What nosotros are interested is the Interfaces tab. Click on the tab and select Enable for I2C. At this point, you can enable additional interfaces depending on your project needs. Click on the OK button to same.

Adjusting Interfaces via Interface Tab

Click on epitome for a closer view.

Nosotros recommend restarting your Pi to ensure that the changes to take effect. Click on the Pi Start Card > Preferences > Shutdown. Since nosotros just demand to restart, click on the Restart push button.

Click on images for a closer view.

raspi-config Tool via Terminal

Similar the SPI peripheral, I2C is not turned on past default. Again, nosotros tin can employ raspi-config to enable information technology.

  1. Run sudo raspi-config.
  2. Use the downward arrow to select 5 Interfacing Options
  3. Arrow downwards to P5 I2C.
  4. Select yeah when it asks you to enable I2C
  5. Also select yes if it asks about automatically loading the kernel module.
  6. Use the right arrow to select the <Finish> push.
  7. Select yes when it asks to reboot.

Enabling I2C on the Raspberry Pi

Raspi-config for I2C

The organization volition reboot. when it comes support, log in and enter the following control

          language:fustigate ls /dev/*i2c*                  

The Pi should respond with

          linguistic communication:fustigate /dev/i2c-1                  

Which represents the user-mode I2C interface.

Utilities

There is a set of command-line utility programs that tin help get an I2C interface working. You can go them with the apt parcel manager.

          language:bash sudo apt-get install -y i2c-tools                  

In particular, the i2cdetect program volition probe all the addresses on a double-decker, and written report whether whatsoever devices are nowadays. Enter the following command in the command line. The -y flag will disable interactive mode and so that yous practice not have to expect for confirmation. The one indicates that we are scanning for I2C devices on I2C bus one (e.1000. i2c-1).

          language:bash i2cdetect -y 1                  

You will become an output from your Raspberry Pi similar to the output beneath.

          language:bash pi@raspberrypi:~/$ i2cdetect -y 1      0  1  2  3  4  v  half dozen  seven  8  9  a  b  c  d  e  f 00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- xxx: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- twoscore: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- sixty: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --                  

This map indicates that there is a peripheral at address 0x60. Nosotros can try to read and write its registers using the i2cget, i2cset and i2cdump commands.

Programming Example

Required Materials

  • The 40-pin Pi Wedge.
  • A Raspberry Pi B+ or Pi 2 Model B unmarried lath computer.
  • A Solderless Breadboard.
  • Some jumper wires.
  • Header pins of your choice.
  • An MCP4725 digital-to-analog converter.

Hookup Table

The brandish was continued to the Pi, via the Pi Wedge, every bit follows.

Raspberry Pi Signal MCP4725
GND GND
three.3V VCC
SCL SCL
SDA SDA

The exam hardware looked like this.

DAC connected to Pi via I2C

DAC on a Breadboard

Sample C++ Program

The following lawmaking writes successive values to the DAC, producing an sawtooth moving ridge at its output pivot.

          linguistic communication:c /****************************************************************************** i2ctest.cpp Raspberry Pi I2C interface demo Byron Jacquot @ SparkFun Electronics> iv/2/2014 https://github.com/sparkfun/Pi_Wedge  A brief demonstration of the Raspberry Pi I2C interface, using the SparkFun Pi Wedge breakout board.  Resources:  This instance makes use of the Wiring Pi library, which streamlines the interface the the I/O pins on the Raspberry Pi, providing an API that is similar to the Arduino.  You tin can larn about installing Wiring Pi here: http://wiringpi.com/download-and-install/  The I2C API is documented here: https://projects.drogon.net/raspberry-pi/wiringpi/i2c-library/  The init call returns a standard file descriptor.  More than detailed configuration of the interface can be performed using ioctl calls on that descriptor. See the wiringPi I2C implementation (wiringPi/wiringPiI2C.c) for some examples. Parameters configurable with ioctl are documented here: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/i2c/dev-interface  Hardware connections:  This file interfaces with the SparkFun MCP4725 breakout lath: https://www.sparkfun.com/products/8736  The board was connected as follows: (Raspberry Pi)(MCP4725) GND  -> GND three.3V -> Vcc SCL  -> SCL SDA  -> SDA  An oscilloscope probe was connected to the analog output pin of the MCP4725.  To build this file, I utilize the command: >  k++ i2ctest.cpp -lwiringPi  And so to run it, starting time the I2C kernel module needs to be loaded.  This tin can exist  washed using the GPIO utility. > gpio load i2c 400 > ./a.out  This will run the MCP through its output range several times.  A rising  sawtooth volition be seen on the analog output.  Development surroundings specifics: Tested on Raspberry Pi V2 hardware, running Raspbian. Building with GCC 4.half dozen.3 (Debian 4.6.3-14+rpi1)  This code is beerware; if you meet me (or whatever other SparkFun employee) at the local, and yous've found our code helpful, please buy united states of america a circular!  Distributed as-is; no warranty is given. ******************************************************************************/  #include <iostream> #include <errno.h> #include <wiringPiI2C.h>  using namespace std;  int main() {    int fd, effect;     // Initialize the interface past giving information technology an external device ID.    // The MCP4725 defaults to address 0x60.       //    // It returns a standard file descriptor.    //     fd = wiringPiI2CSetup(0x60);     cout << "Init result: "<< fd << endl;     for(int i = 0; i < 0x0000ffff; i++)    {       // I tried using the "fast write" command, only couldn't get it to work.         // It's not entirely obvious what's happening behind the scenes as       // regards to endianness or length of data sent.  I think it'southward simply        // sending one byte, when we actually need two.       //       // Then instead I'm doing a sixteen bit register access.  Information technology appears to        // properly handle the endianness, and the length is specified past the        // phone call.  The just question was the annals address, which is the        // chain of the command (010x = write DAC output)        // and power down (x00x = ability upwards) bits.       result = wiringPiI2CWriteReg16(fd, 0x40, (i & 0xfff) );        if(result == -i)       {          cout << "Error.  Errno is: " << errno << endl;       }    } }                  

Build information technology and link information technology to wiringPi using the post-obit command.

          language:bash yard++ i2ctest.cpp -fifty wiringPi -o i2ctest                  

When yous run i2ctest, the DAC volition produce an analog sawtooth moving ridge for a few seconds.

Sawtooth waveform on oscilloscope

Waveform equally measured at the OUT pivot

Sample Python Programme

          linguistic communication:python # i2ctest.py # A cursory demonstration of the Raspberry Pi I2C interface, using the Sparkfun # Pi Wedge breakout board and a SparkFun MCP4725 breakout lath: # https://world wide web.sparkfun.com/products/8736  import smbus  # I2C channel 1 is connected to the GPIO pins aqueduct = 1  #  MCP4725 defaults to address 0x60 address = 0x60  # Annals addresses (with "normal manner" power-downwards $.25) reg_write_dac = 0x40  # Initialize I2C (SMBus) bus = smbus.SMBus(aqueduct)  # Create a sawtooth wave 16 times for i in range(0x10000):      # Create our 12-chip number representing relative voltage     voltage = i & 0xfff      # Shift everything left past 4 $.25 and split bytes     msg = (voltage & 0xff0) >> 4     msg = [msg, (msg & 0xf) << four]      # Write out I2C command: address, reg_write_dac, msg[0], msg[1]     motorbus.write_i2c_block_data(address, reg_write_dac, msg)                  

Salvage the program with a name like i2ctest.py, and run it with the command:

          language:bash python i2ctest.py                  

Y'all should encounter a sawtooth wave announced on the DAC output. If you connect an oscilloscope, you lot should become an paradigm similar the one shown in the C++ example. Note that Python is much slower than C/C++! The period of the sawtooth wave in the C++ example was effectually 100 ms whereas the menstruation of the wave in the Python example was close to 1.8 seconds.

Be enlightened that SMBus is a protocol layer separate from but built on top of I2C. Some features of I2C may non exist available with SMBus. For example, SMBus cannot handle clock stretching, so sensors that crave it to communicate volition not work with the smbus packet.

To learn more about the smbus protocol, see the official kernel documentation.


williamslearrigh.blogspot.com

Source: https://learn.sparkfun.com/tutorials/raspberry-pi-spi-and-i2c-tutorial/i2c-on-pi

0 Response to "Can Get I2c or Spi to Work on Raspberry Pi"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel