A Swift library for the MCP3008 10-bit SPI ADC
This simple library reads the values produced by the MCP3008 10 bits SPI-driven ADC. This component is able to convert analog signals (with a voltage range defined by Vref, most of the times you'll connect Vref and Vdd to 5V) to an integer value between 0 and 1023. ADCs are extremely useful for example for boards like the RaspberryPis that don't have their own analog input pins (like Arduinos or traditional embedded boards).
Every board supported by SwiftyGPIO: RaspberryPis, BeagleBones, C.H.I.P., etc...
To use this library, you'll need a Linux ARM board with Swift 3.x/4.x.
The example below will use a RaspberryPi 2 board but you can easily modify the example to use one the the other supported boards, a full working demo projects for the RaspberryPi2 is available in the Examples
directory.
The first thing we need to do is to obtain an instance of SPIInterface
from SwiftyGPIO and use it to initialize the MCP3008
object:
import SwiftyGPIO
import MCP3008
let spis = SwiftyGPIO.hardwareSPIs(for:.RaspberryPi2)!
let spi = spis[0]
let m = MCP3008(spi)
Then just use readValue
to read the current converted value (0...1023) from one of the analog inputs:
m.readValue(0) //CH0 pin
Reading a value from an unconnected channel (floating input) will give meaningless values, so, be sure to read from the right input.
The library also support virtual spis using bit-banging:
let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPi2)
var cs = gpios[.P27]!
var mosi = gpios[.P22]!
var miso = gpios[.P4]!
var clk = gpios[.P17]!
var spi = VirtualSPI(mosiGPIO: mosi, misoGPIO: miso, clockGPIO: clk, csGPIO: cs)
let m = MCP3008(spi)
for i in 0...7 {
print("Value for channel ",i,": ",m.readValue(for: i) << 1) //Shift required to obtain the correct value
}
Please refer to the SwiftyGPIO readme for Swift installation instructions.
Once your board runs Swift, if your version support the Swift Package Manager, you can simply add this library as a dependency of your project and compile with swift build
:
let package = Package(
name: "MyProject",
dependencies: [
.Package(url: "https://github.com/uraimo/MCP3008.swift.git", majorVersion: 2),
...
]
...
)
The directory Examples
contains sample projects that uses SPM, compile it and run the sample with ./.build/debug/TestMCP
.
If SPM is not supported, you'll need to manually download the library and its dependencies:
wget https://raw.githubusercontent.com/uraimo/MCP3008.swift/master/Sources/MCP3008.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SwiftyGPIO.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/Presets.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SPI.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SunXi.swift
And once all the files have been downloaded, create an additional file that will contain the code of your application (e.g. main.swift). When your code is ready, compile it with:
swiftc *.swift
The compiler will create a main executable.