MCP3008 over SPI

Post Reply
bsodroid
Posts: 4
Joined: Tue Sep 11, 2018 2:33 am
languages_spoken: english
ODROIDs: ODroid-C2
Contact:

MCP3008 over SPI

Unread post by bsodroid » Tue Sep 11, 2018 2:49 am

Hello everyone. I've been tinkering with the SW SPI available through the Odroid-C2 in order to control a MCP3008 Analog to Digital Converter. Unfortunately, I've found getting it to work properly is a bit more complicated than I had imagined, and I'm hoping to get some help.

MCP3008: https://cdn-shop.adafruit.com/datasheets/MCP3008.pdf

Work so far:

Followed the wiki to load driver (modprobe)
Verified drivers are loaded (lsmod | grep....)

Did the loopback test on spidev0.0, output is as follows:

Code: Select all

spi mode: 0
bits per word: 8
max speed: 9600 Hz (9 KHz)

01 02 03 04
From here, i've tried the following:

Unofficial wiringPi-Odroid and modified spiSpeed.c to work with the device. output:

Code: Select all

+-------+--------+----------+----------+-----------+------------+
|   MHz |   Size | mS/Trans |      TpS |    Mb/Sec | Latency mS |
+-------+--------+----------+----------+-----------+------------+
|     1 |      1 |    0.040 |  25000.0 |   0.19073 |    0.03200 |
|     1 |      2 |    0.050 |  20000.0 |   0.30518 |    0.03400 |
|     1 |      4 |    0.100 |  10000.0 |   0.30518 |    0.06800 |
|     1 |      8 |    0.170 |   5882.4 |   0.35903 |    0.10600 |
|     1 |     16 |    0.340 |   2941.2 |   0.35903 |    0.21200 |
|     1 |     32 |    0.670 |   1492.5 |   0.36439 |    0.41400 |
|     1 |     64 |    1.320 |    757.6 |   0.36991 |    0.80800 |
Tested communication with MCP3008 with the following:

Setup:
CH0 - 1.1V
VREF - 3.3V GPIO 1
VDD - 3.3V GPIO 1
DIN - Odroid GPIO 19
DOUT - Odroid GPIO 21
CS - GND (tested at 3.3V as well)
CLK - Odroid GPIO 23
Grounds where appropriate


Code with python + spidev:

Code: Select all

#!/usr/bin/python3

import time
import spidev
import struct


spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 9600
spi.bits_per_word = 8
try:
        while True:
                resp = spi.readbytes(1)
                print("output: " + str(resp[0]))
                time.sleep(.1)
except KeyboardInterrupt:
        spi.close()
Result:

Code: Select all

output: 255
output: 255
output: 255
Running spidev with this wiring result:

Code: Select all

odroid@odroid64:~$ sudo ./spidev_test -D /dev/spidev0.0 -s 9600 -b 8
spi mode: 0
bits per word: 8
max speed: 9600 Hz (9 KHz)

FF  FF  FF  FF
I've hooked a scope up to the CLK signal and got nothing out of it, just residual noise.

I'd really prefer to get this working with python rather than C, but I'd take anything over nothing at this point.


Thanks for your help, happy to add any other data that's needed

-BS

bsodroid
Posts: 4
Joined: Tue Sep 11, 2018 2:33 am
languages_spoken: english
ODROIDs: ODroid-C2
Contact:

Re: MCP3008 over SPI

Unread post by bsodroid » Tue Sep 11, 2018 3:58 am

All,

Managed to get this one to work.

Code

Code: Select all

#include <wiringPi.h>
#include <stdio.h>
#include <unistd.h>

#define BASE 300
#define SPI_CHAN 0

#include "mcp3004.h"

int main(){

        if(wiringPiSetup() == -1){
                return 1;
        }

        int chan;
        int mcp_answer;
        mcp3004Setup(BASE, SPI_CHAN);

        while(1){

                for(chan = 0; chan < 8; chan++){
                        mcp_answer = analogRead(BASE + chan);
                        printf("output: %d ", mcp_answer);
                }
                sleep(1);
                printf("\n");
        }
}
Output:

Code: Select all

output: 345 output: 0 output: 0 output: 0 output: 0 output: 0 output: 0 output: 0

User avatar
odroid
Site Admin
Posts: 29700
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Contact:

Re: MCP3008 over SPI

Unread post by odroid » Tue Sep 11, 2018 10:18 am

Glad to hear the 8 channel ADC works on C2 SPI. :D

Post Reply

Return to “Hardware and peripherals”

Who is online

Users browsing this forum: No registered users and 2 guests