element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
BitScope
  • Products
  • Manufacturers
  • BitScope
  • More
  • Cancel
BitScope
Blog BitScope Micro Logic Analyser Demystifies SPI on the Pi
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join BitScope to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: bitscope
  • Date Created: 9 Jun 2015 6:13 AM Date Created
  • Views 5355 views
  • Likes 0 likes
  • Comments 0 comments
  • oscillscopes
  • test
  • bitscope
  • measurement
  • logic_analyzer
  • bitscope_micro
Related
Recommended

BitScope Micro Logic Analyser Demystifies SPI on the Pi

bitscope
bitscope
9 Jun 2015

SPI – Serial Peripheral Interface Demystified

This article cross-posted from Raspi.Tv with permission and our thanks!


I was in need of an interesting signal to check out the logic analyser functionality of the BitScope micro. So, another of my current RasPiORegistered Beta boards was used as a convenient way to connect an mcp3008 analog to digital converter (ADC) to the Pi and the scope.

RasPiO Analog board + BitScope probes connected

RasPiO Analog board + BitScope probes connected

I don’t claim to be an expert on SPI, but having spent the best part of a day playing with it, the mcp3008 data sheet and the BitScope Micro, I understand it a lot better now. Four pins are used…

  • CEO – the chip select pin
  • SCLK – the clock pin
  • MOSI – Master Out, Slave In i.e. the Pi’s SPI output to mcp3008 input
  • MISO – Master In, Slave Out i.e. the Pi’s SPI input from mcp3008 output

…and the way it works, at least my interpretation of it, follows the next large picture.

To Make an Interesting Pulse Pattern

Connecting 0V (00000000) or 3V3 (11111111) to the ADC gave a flat, boring MISO (ADC output) trace – All LOW (0V)…

MISO output at 0V

MISO output at 0V

…or all HIGH (3V3)…

MISO output at 3V3

MISO output at 3V3

So I thought I’d try and get an alternating signal, e.g. 1010101010

To get the 1010101010 reading from the ADC, I had to convert 1010101010 to base 10, which is 682. Then, using a potentiometer, patience and Python, I tweaked the ADC input so the reading was exactly 0682 on the ADC (2.2V). Then I used the BitScope logic analyser with four probes hooked up to the SPI pins to capture the SPI signals. (L0 to MISO, L1 to MOSI, L2 to SCLK & L3 to CE0.)

For convenience, I connected them in the same order as the diagram in the mcp3008 data sheet (white overlay in the screenshot below). This makes it a bit easier to follow. (Although it’s a bit geeky if you’ve never done it before, and might take a little while to understand it clearly.) This is what the SPI signal traces look like when the ADC is reading a value of 0682. You can click the image to enlarge it, which makes the darkish colours of the SCLK & MOSI traces a bit easier to see.

mcp3008 input/output signals for ADC value 0682 (1010101010)

mcp3008 input/output signals for ADC value 0682 (1010101010)

Here’s a Walkthrough of the Diagram

When CE0 is brought LOW, the mcp3008 is activated (it is low for the whole of this screenshot, as that’s what I set as the BitScope trigger).

The Pi’s SPI clock pin, SCLK sends out 3 bytes (8 pulses each, 6 microseconds apart), which control the timing of the signals to and from the chip/pi.

On the 8th pulse of the first byte, MOSI sends a “START” bit, followed by four more bits (SGL, D2, D1, D0) to tell the ADC which mode and channel to use. In this case, single mode (1), channel 0 (0, 0, 0).

Thereafter, MOSI is ignored for the rest of this cycle.

Then the ADC reads the value of the voltage connected to its channel 0 input during the next clock pulse. After that, it uses the MISO port to output a null bit (0), followed by 10 data bits (B9-B0). These 10 bits are the binary value of the ADC reading. In our case, it’s 1010101010. You can see this in the bottom-right part of the logic trace (enlarged below)…

mcp3008 output of 10101010

mcp3008 output (MISO) of 10101010

Note that B7 is wider because it spans the ‘gap’ between the second and third ‘bytes’ from the SCLK pin. This is expected, and appears in the data sheet diagram too.

When all the data has been ‘clocked out’ of the mcp3008, the CE0 chip select pin on the Pi is brought HIGH to end the transaction and deactivate the mcp3008.

It Was Nice to Confirm it

I’d seen these diagrams in data sheets before and only partially understood them. I’d ‘done’ the software side of it before. But through probing, poking, tweaking and adjusting, I now think I understand how the SPI protocol works in hardware for the mcp3008. To my mind, that’s a day very well spent! Education is priceless.

To those who think it’s all a bit weird, the main draw for many in science, engineering and programming is that “YES” (punch the air) feeling when you understand how or why something works and how you can “fiddle” with it to influence or control what it does.

And it’s all a part of my plan to take over the world. MwoaHaHaHaHaaaaa!

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube