*Preliminary* SPI on the Pi: Communicating With a SPI 3-axis Accelerometer Using a Raspberry Pi

Introduction: *Preliminary* SPI on the Pi: Communicating With a SPI 3-axis Accelerometer Using a Raspberry Pi

Step by step guide on how to setup Raspbian, and communicate with a SPI device using bcm2835 SPI library (NOT bit banged!)

This is still very preliminary... I need to add better pictures of physical hookup, and work through some of the awkward code.

Step 1: Starting With a Blank SD Card, Download Raspbian Image, and Install Onto SD Card

Visit http://www.raspberrypi.org/downloads for instructions on how to install Raspbian

I downloaded:
Raspbian image, and used
Win32DiskImager to install on SD card

There's also more information at http://elinux.org/RPi_Easy_SD_Card_Setup

Step 2: Connect Raspberry Pi to TV/Monitor, and Run Through Initial Setup

(Internet connection not required yet)

Set timezone
enable SSH

Then, Finish.

Terminal code:

Step 3: Optional: Operate Pi Headless

Excellent tutorial at

I use Putty (Windows) or Terminal (Mac) to connect with SSH

Step 4: Recommended: Update OS

Terminal Code:
sudo apt-get update
sudo apt-get upgrade

Step 5: Optional: Setup IP Address E-mailer

I've set up my Pi to e-mail me it's IP address each time it boots.  This makes my life easier when I need to remote login using SSH.

Excellent tutorial at

Step 6: Optional - Setup VNC

Excellent tutorial at

I didn't go through the entire tutorial... just the following steps:
$ sudo apt-get install tightvncserver
$ tightvncserver
$ vncserver :1 -geometry 1200x800 -depth 24

And, I created a script to keep my typing to a minimum.

Step 7: Install BCM2835 SPI Library


Excellent documentation (and examples) at http://www.open.com.au/mikem/bcm2835

Terminal code:
// wget http://www.open.com.au/mikem/bcm2835/bcm2835-1.5.tar.gz; // My Pi can't figure out this URL - unable to resolve host name?
tar xvfz bcm2835-1.5.tar.gz;
cd bcm2835-1.5;
sudo make install

Step 8: Get ADXL362 SPI Example

Note: Code is still very basic... need to improve

get ADXL362_RaspPi from https://github.com/annem/ADXL362_RaspPi
(How to do this on Pi, using wget?  I'm having trouble with this... "unable to resolve host address 'github.com'")

Step 9: Phyiscally Connect ADXL362 Breakout to Raspberry Pi GPIO

More details to come...

More info about ADXL362 (ultra low power 3-axis accelerometer) at analog.com/ADXL362

Connect 3v3, GND, SPI0 MOSI, SPI0 MISO, SPI0 SCLK, SPI0 CE0 N on Raspberry Pi to
VDDand VIO, GND (2), MOSI, MISO, SCLK, and CSB on ADXL362 Breakout board.

Step 10: Compile and Run ADXL362_RaspPi

terminal code:
gcc -o ADXL362_RaspPi -I ../bcm2835-1.5/src ../bcm2835.c ADXL_RaspPi.c
sudo ./ADXL362_RaspPi

Which I combines into a script called compileADXL362.

Be the First to Share


    • Hot Glue Speed Challenge

      Hot Glue Speed Challenge
    • Pets Challenge

      Pets Challenge
    • Remote Control Contest

      Remote Control Contest



    4 years ago

    Any update to this instructable? For example,

    Regarding Step #7, is there a way to see if installed correctly? I rebooted but don't see anything new in /dev

    Also in Step #8, I was able to download via:


    (sudo apt-get install unzip)

    unzip master.zip

    gcc -o ADXL362_RaspPi -I ../../bcm2835-1.5/src ../src/bcm2835.c ADXL362_RaspPi.c

    However it doesn't compile due to errors (see image):

    Finally, any thoughts on using auxspi0/1 instead of spi0? Apparently the auxspi peripherals have other cool features.. including the ability to transmit 1-32 bits at a time (instead of just 8 or 9 with spi0)!

    Unfortunately, only spi0 seems to be supported by the common driver/tools (spincl, WiringPI and the 'bcm2835' c lib)

    I did find one driver but seems like there might be an issue with it in Raspian/Jessie8 (4.4.50+):


    I'd still like to give it a try, but is the process the same? Does loading the driver module require changes the device tree / overlays?


    9 years ago on Step 2

    Awesome instructable. If your RasPi browser nameserver isn't pulling up domains, try:

    # sudo cat >> /etc/resolv.conf

    This adds the Google public DNS server. You can add any nameserver you want instead of

    Others can build on this to do hardware SPI instead of bitbanging, saving digital pins and making things faster, cool :)


    Reply 7 years ago on Introduction

    Ctrl-Z is ugly... When you give instructions, you try to make it 1d10t-proof:

    grep "^nameserver.*$" /etc/resolv.conf > /dev/null ; if [ "${?}" -ne "0" ] ; then echo -en "\nnameserver\n" >> /etc/resolv.conf ; fi

    That way, you are sure the line is added only once in the file with a simple copy&paste and can be run as many times without crippling your DNS resolver. is Google's openDNS but the DHCP from your DSL router should have pointed you to a fast accurate DNS, usually looking like (your router is usually a DNS server too in home installations) ; if your DNS is not set, it's probably you've already been fiddling with static manual IP configuration without being too sure of what you were doing (to say it mildly). is convenient, but you should never use it for normal operation.


    Reply 9 years ago on Introduction

    Thanks so much for the tip! I can use all the advice I can get ;)

    I'll give it a try, and update the tutorial if is works.


    9 years ago on Introduction

    Hi All.
    Before step 10 i suggest to check if in /etc/modprobe.d/raspi-blacklist.conf you have blacklisted spi... I had. The file should look like this:

    # blacklist spi and i2c by default (many users don't need them)
    #blacklist spi-bcm2708
    #blacklist i2c-bcm2708


    9 years ago on Step 10

    I had to update the following command in order to get this tutorial to work:
    (1) updated inputs (denoted by -I in command)
    (2) updated the path from ../bcm2835.c to ../bcm2835-1.5/src/bcm2835.c
    (3) updated file name from ADXL_RaspPi.c to ADXL362_RaspPi.c

    gcc -o ADXL362_RaspPi -I ../bcm2835-1.5/src ../bcm2835.c ADXL_RaspPi.c


    9 years ago on Step 8

    -Try this to do it from command line:

    1) create a specific directory:
    mkdir ADXL362_RaspPi
    cd ADXL362_RaspPi/

    2) install Git if you don't have it:
    sudo apt-get install git

    3) create a repository and pull from Github:
    git init .
    git add .
    git pull https://github.com/annem/ADXL362_RaspPi.git


    9 years ago on Step 5

    You can avoid the email by using IPv6 Link Local.
    Your raspberry PI will always have the same IPv6 Link Local address every time it boots and it's determined from it's mac address.

    For example, if your PI has a MAC address of 23:45:67:89:ab:cd, then it's link local address will be fe80::2145:67ff:fe89:abcd.

    Converting the MAC to link local is as follows: Take the first three octets of MAC and insert FFFE and then the last 3 octets. (all values in hex).
    For 11:11:11:22:22:22 this results in 1111:11FF:FE22:2222

    Then, invert the 0x02 bit of the first octet: 1311:11ff:fe22:2222.

    Finally place fe80:: in front fe80::1311:11ff:fe22:2222

    Now, to communicate with that, you will need to specify the interface ID of the outbound interface on your machine. On rational systems, this is the name of the interface, such as "eth0" on Linux or "en0" on Mac, "fxp0" on BSD, etc. On windows boxes, it's unfortunately a number which has little rime or reason to it, but you should be able to get the correct number from ipconfig /all.


    Reply 9 years ago on Step 5

    I'll have to tinker with that... thanks! Not familiar with IPv6, yet. Thanks for the tip!