I'm using a MCP3008 8ch 10bit ADC to expand the Analog Inputs for the project I'm working on. The only library I can find for it is here the library is ported from Adafruit's Raspberry Pi library here. From what I can tell the library uses bitbanging as opposed to hardware SPI in order to communicate. I would like to use hardware SPI, but don't have the skills to write a library to communicate with it myself. Are there any resources for how to write a library like that, or anything I can read about on how to convert a library from bitbanging to hardware SPI?

  • What Arduino is this for? – Nick Gammon Mar 27 '16 at 4:25
  • It's for a PCB I designed based on the Atmega 1284 with hardware support added to the IDE with MightyCore found here: github.com/MCUdude/MightyCore – TheAutomator Mar 27 '16 at 4:30
  • Typically, the library has a "sendByte" function in which it sends through "software spi" you would only have to re-write that. – Paul Mar 27 '16 at 13:53
  • The library uses some weird form of SPI. As it sends only 5 bits, and then reads 12 bits. SPI is normally a multiple of 8 bits. – Gerben Mar 27 '16 at 15:53

The standard AVR library has SPI handling. See, for example: http://www.gammon.com.au/spi

You don't have to write a library if you don't want, just communicate directly using SPI.

The datasheet for the MCP3008 has some tips for using an 8-bit SPI system to communicate with the device. In particular this chart:

MCP3008 timings

You can see from that, that you will need 3 x SPI.transfer. I don't have one here to test with, but the code would look like this:

digitalWrite (SS, LOW);  // select device
int result;
SPI.transfer (0x01);  // start bit
result = SPI.transfer (0b10000000);  // single, ch0
result = (result & 0b11) << 8;  // move to high-order byte
result |= SPI.transfer (0);   // lower order byte
digitalWrite (SS, HIGH);  // deselect

Untested, but this should be roughly it.


Turns out my google-fu needs work. I found this that works great using hardware SPI! Thanks everyone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.