I am using this ADC connected to the channel 0 of my Pi3 board for reading values from a sensor. In IoT core, I was using a very simple code for testing the my device, and it worked fine. The C# code is the following:

// Initialize device
var settings = new SpiConnectionSettings(0);
settings.ClockFrequency = 1000000;
settings.Mode = SpiMode.Mode0;
// Get and intialize the SPI controller
var controller = await SpiController.GetDefaultAsync();
spi_dev = controller.GetDevice(settings);

// do stuff

byte[] read_buffer = new byte[3];
byte[] write_buffer = new byte[3] {0xDF, 0x00, 0x00 };
spi_dev.TransferFullDuplex(write_buffer, read_buffer);

Currrently, I'm trying to do the same in Raspbian, for comparing the performance. So far, I tried C/C++ libraries wiringPi and bcm2835, as well as Python library SpiDev.

I enabled the SPI connection using the raspi-config utility (I checked that /boot/config.txt value dtparam=spi=on is uncommented). However, with any of the mentioned libraries, I always get zero as a response. The different scripts I am trying are the following:

Using bcm2835:

#include <bcm2835.h>
// ...
// Clock of 1.5625MHz on RPI3 

bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);

// Send the control byte to the slave and simultaneously read 3 bytes back.
char send_data[3] = {0xDF, 0x00, 0x00};
char read_data[3] = {0};
bcm2835_spi_transfernb(send_data, read_data, 3);

using wiringPi:

#include <wiringPiSPI.h>

int fd, result;
unsigned char buffer[100];

fd = wiringPiSPISetup(0, 1000000);

buffer[0] = 0xDF;
result = wiringPiSPIDataRW(0, buffer, 3);

and using SpiDev:

import spidev

#initialise SPIDev
spi.max_speed_hz = 1000000

ctrl_byte = 0xDF
send = [ctrl_byte, 0x00, 0x00]
response = spi.xfer(send)
  • For some reason my comment asking for a photo has been deleted - not by me. We need a photo of the connections if you want any help. – joan Aug 10 '18 at 19:24

Use BCM2835 "library" with great caution - "transfer" function does not read SPI slave data.

| improve this answer | |
  • Could you provide a library, a function, or an example with which I can read back values from SPI? – Jalo Aug 10 '18 at 11:09
  • Well, yes and no. I have been sidetracked for few months from my main C++ coding task and my code is not for public view anyway - too messy. Maybe in couple of months when the project is done - really sorry I cannot be of more help. – Jan Hus Aug 10 '18 at 18:54

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.