0
\$\begingroup\$

I am trying to get a voltage reading from a MAX1142BCAP+ with a Raspberry Pi 4B. I am just using the default circuit at the bottom of the datasheet and this code for SPI in Python:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import spidev

# ams or aquisition mode select can either be 0 or 1, 1 being long, 0 being short

spi = spidev.SpiDev()

def adc_spi(ams):
    spi.open(0, 0)
    spi.max_speed_hz = 500000
    spi.mode = 0b00
    spi.writebytes2([0b11100000])
    pressure = spi.readbytes(6)
    print(pressure)
    spi.close()

adc_spi(0)

When I run this code I get these outputs in console without changing the voltage on Ain.

pi@gauge:~ $ python3 adc1.py
[40, 0, 0, 0, 0, 0]
pi@gauge:~ $ python3 adc1.py
[64, 0, 0, 0, 0, 0]
pi@gauge:~ $ python3 adc1.py
[64, 0, 0, 0, 0, 0]
pi@gauge:~ $ python3 adc1.py
[80, 0, 0, 0, 0, 0]
pi@gauge:~ $ python3 adc1.py
[80, 0, 0, 0, 0, 0]

When I analyze the digital outputs with a logic analyzer I get outputs like these:

enter image description here

These outputs change each time I run this but the values don't match the voltage I give Ain. These outputs don't match what the datasheet says I should get, either.

Some things I think the problem could be:

  • The fact I am not resetting between runs maybe.
  • The SPI mode although I have read the datasheet quite a few times by now.

Other then that I have no clue.

I have reconnected everything several times and tested each of my jumpers. I have tried another chip just in case this one was broken. I know this is a noob question this is my first time using SPI any insight you could give for trouble shooting this would be appreciated. Or if you think its probably a lost cause and I should just buy a I2C ADC that would be helpful too.

This is the datasheet I am using.

\$\endgroup\$
  • 2
    \$\begingroup\$ So what is the voltage at AIN? Does the ADC output change in a predictable way if you make large changes to the input voltage? What happens if you call adc_spi() several times in the same program, without resetting the SPI interface? What is the purpose of the ams parameter? \$\endgroup\$ – Elliot Alderson Jul 7 at 16:53
  • \$\begingroup\$ What are the "jumpers" you refer to? Is this assembled on some kind of wireless breadboard? What is the source of the input voltage? \$\endgroup\$ – Elliot Alderson Jul 7 at 16:54
  • \$\begingroup\$ I have tried a range of voltages from 0 to 12 volts and the output is always 40, 64, or 80. The jumpers I used are just wires to connect the pi. If i call adc_spi multiple times. i get [64, 0, 0, 0, 0, 0] [80, 0, 0, 0, 0, 0] [64, 0, 0, 0, 0, 0] [64, 0, 0, 0, 0, 0] [64, 0, 0, 0, 0, 0] [64, 0, 0, 0, 0, 0] [64, 0, 0, 0, 0, 0] \$\endgroup\$ – nomad2415 Jul 7 at 17:11
  • \$\begingroup\$ The AMS parameter selects between internal sampling modes for the ADC; 0 is 24 clocks per conversion and 1 is 32. @ElliotAlderson \$\endgroup\$ – nomad2415 Jul 7 at 17:18
  • \$\begingroup\$ Why does your trace for MOSI not match the 0b11100000 value you're writing? \$\endgroup\$ – brhans Jul 7 at 18:06

Your Answer

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

Browse other questions tagged or ask your own question.