1

Pi model: Pi 3 A+

Os: Raspbian GNU/Linux 9 (stretch) <- it's stretch lite

SPI: On

Loop back test:

spi mode: 0x0
bits per word: 8
max speed: 500000 Hz (500 KHz)
TX | FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F0 0D  | ......@....�..................�.
RX | FF FF FF FF FF FF 40 00 00 00 00 95 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF F0 0D  | ......@....�..................�.

Wiring: enter image description here

Code:

package main

import (
  "log"

  rpio "github.com/stianeikeland/go-rpio"
  "golang.org/x/exp/io/spi"
)

const (
  mosi = rpio.Pin(10) //physical 19
  miso = rpio.Pin(9)  //physical 21
  clk  = rpio.Pin(11) //physical 23
  ce0  = rpio.Pin(8)  //physical 24
  ce1  = rpio.Pin(7)  //physical 26

)

func main() {
  if err := rpio.Open(); err != nil {
    log.Println(err)
    return
  }

  defer rpio.Close()

  pins := []rpio.Pin{mosi, miso, clk, ce0, ce1}

  for _, pin := range pins {
    pin.Low()
  }

  dev, err := spi.Open(&spi.Devfs{
    Dev: "/dev/spidev0.0", Mode: spi.Mode0, MaxSpeed: 500000})

  if err != nil {
    log.Println("SPI error", err)
    log.Println("Did you forget to enable SPI interface?")
    return
  }
  defer dev.Close()

  out := []byte{1, 8 << 4, 0}
  in := make([]byte, 3)

  if err := dev.Tx(out, in); err != nil {
    log.Println("SPI read/write error. ", err)
    return
  }

  code := int(in[1]&3)<<8 + int(in[2])

  log.Println(code)
}

Voltage on ch 0 is 1.49, no conversion done by the chip. Returns 0 on ch 0.

Checked Vdd and Vref with multimeter using the analog and digital grounds.

Voltage reads 3.3

A year and a half ago the setup above worked with a pizero on jessie lite.

I tested 3 different mcp3008 chips with the same result.

Also tested on a pizero, no success.

Where is my mistake?

3
  • Go through a tutorial e.g. this one to confirm that your setup is working. Jan 17, 2019 at 9:39
  • 1
    Software SPI from the tutorial linked works, hardware SPI returns same results as my setup. Would rather have hardware SPI working and avoid building a software implementation Jan 18, 2019 at 12:43
  • I'm not saying you should use SW SPI, but the fact that it works is very important: it confirms that your wiring is correct. Sorry I won't be able to help on this one, but +1 and best of luck getting an answer. Jan 18, 2019 at 13:18

1 Answer 1

1

Solved it.

MISO and MOSI were wired wrong.

Also the SPI package I used was a bit old.

Rewrote the code using this package "github.com/stianeikeland/go-rpio"

Here is the new working code:

package main

import (
  "log"
  "time"

  rpio "github.com/stianeikeland/go-rpio"
)

func main() {
  if err := rpio.Open(); err != nil {
    log.Println(err)
    return
  }

  defer rpio.Close()

  if err := rpio.SpiBegin(rpio.Spi0); err != nil {
    log.Println(err)
    return
  }

  rpio.SpiSpeed(1000000)
  rpio.SpiChipSelect(0)
  ticker := time.NewTicker(time.Millisecond * 1000)

  //change 0 from range 0 to 7 to read from different channels
  channel := byte(0)
  for range ticker.C {
    data := []byte{1, (8 + channel) << 4, 0}

    rpio.SpiExchange(data)

    code := int(data[1]&3)<<8 + int(data[2])

    //channels
    log.Println(code)

    //Vdd and Vref are at 5v. Change *5 to *3.3 if you are
    //powering the chip with 3.3v
    voltage := (float32(code) * 5) / 1024

    log.Println(voltage)

  }
  defer rpio.SpiEnd(rpio.Spi0)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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