Uncategorized

load older messages load to my last message full transcript highlights

Feeds

Feeds
yst 11:43
1

Q: MCP3008 outputting just 0

Aubrey ChampagneI’ve been messing with raspberry pi, and I am learning about interfacing it with analog devices. I am using the MCP3008 for that and following this tutorial here. I wired it up like the pictures below: I did all of the required things to set up spi devices such as: 1) enable spi in raspi-conf…

tlfong01

tlfong01
2427
Hi @Aubrey Champagne, Welcome and nice to meet you. Ah, let me see. (1) You setup looks correct. (2) For newbies, I usually suggest to do SPI “loop back” test first, to make sure SPI software and hardware (hardware wiring correct, no poor electrical connect, broker jumper wires (sometime bent too often and broken inside and not visible outside).
If you have a multi-meter, you can try “open, short” tests, ie, all point to point connection are shorted (very low resistance, approaching zero), and neighbour points are open, ie not “stuck” together, therefore very very high resistance, approaching 10Mega ohm.
You can google for “UART”, “serial”, “SPI” loop back test to get a rough idea what is meant by loop back, and the setup (For SPI, MOSI (output) connected/shorted to MSIO (input). then send something out to MSOI, and read back from MISO. If send characters are echoed, loopback to input, then basic steup is OK. WARNING, usually basic out to input short test does NOT check CS pin operation, so not 100% proof.
For spi loopback tests in Rpi python 3.5x and 3.7x, you can search this forum using key words “SPI”, “loopback”. If you add the search word “tlfong01” you might find a couple of my answers with loopback python test programs, colourful waveforms illustrating the CLK, MOIS, MISO, CSn waveforms. You don’t need to understand the details of the programs. My programs are “plug and play/run” no library is required. Though you need to “uncomment” to select slight variation of loopback, say one, two, or three characters, or control characters such a <control x> etc. Happy testing, cheers!
Aubrey Champagne
Aubrey Champagne
@tlong01 I performed the loopback test, and i got hexadecimal characters, just like the tutorials said I should.
@joan I hooked it straight to 3.3V, and still got 0
tlfong01

tlfong01
2427
Hi @Aubrey Champagne. How nice you have made a successful SPI loopback. Congratulations! Let me tell you why this should be congratulated and the significance or implications. (1) SPI or UART loopback filters away many possible/potential problems, include (a) bad power supply (too low), (b) bad wiring, too long wiring, acting as antenna absorbing noise, especially at high frequencies,
(b) intermittently poor contact connections (better use cable tie to do strain relief). BTW this loopback test is very useful for trouble shooting later, in case your MCPO3008 breaks down, or when you squeeze more SPI devices and crash things.
Aubrey Champagne
Aubrey Champagne
@tlfong01 I have an extra pi, so tomorrow I am going to use a clean version of Raspian to find the problem. Hopefully it works
tlfong01

tlfong01
2427
I read your tutorial and found the critical function read ADC result is bit complicated, with the following two statements: (1) r = spi.xfer2([1, 8 + adcnum << 4, 0]) (2) data = ((r[1] & 3) << 8) + r[2]. This is the most critical part. You need to understand every word, every symbol, =, &, <<, + etc then you can troubleshoot in case you made a typo error. It is unlikely that your Rpi is bad. It is more likely the human newbie makes a stupid mistake somewhere.
Anyway, you already passed the SPI loopback test, so you are already a junior ninja. Perhaps I should give you another test called “Ping MCP3008 Test”, to help you upgrade to middle level ninja! 🙂
Aubrey Champagne
Aubrey Champagne
yst 11:43
@tlfong01 you can ping the device? I thought there was no way of detecting it through the pi
tlfong01

tlfong01
2427
@AubreyChampagne Well, it depend what do you mean by “ping”. Bats ping by sending and receiving signal in the form ultrasound waves. So does submarine pings their friends or foes. For UART/I2C/SPI devices, there are different way of pinging (1) read the “device id” byte in the device id byte. For example, I am playing with AXDL345 accelerator, which has a id register (address 0, if I remember correctly) with an unique byte 0x5b or 0x5e, if I remember correctly.
So pinging simply means reading that register. Another example is MCP23017 or MCP23s17. I usually write to the config reg (address 0x00) a config byte such as setting input or output of its GPIO, then immediately read back, to make sure what I have written can be read back. Some devices have default POR reg contents. So you can just read the default POR byte, … You might like to read MCP3008 datasheet to let me know how to ping it.
By the way, so you have used loop back to make sure basic wiring, timing etc are ok to do spi send receive bytes with SCL, MOSI, and MSIO. But CSn’s are not checked. You can read my old answers on UART/SPI loop back posts the following “Check CSx” trick, WITHOUT using scope, just use a multi-meter. The trick is repeatedly, say 200,000 times doing loopback or repeatedly send a byte (function included in my loopback demo programs).
And at the same time check the voltage at CSn, if CSn jumping up and down, multi-meter should read around 3V3/2 == 1.7v (actuslly it is not simple average, but rms) Of course you can also use your cheapy meter to check other SPI pins, …
Now you can try to test (1) CS pin is working, (2) ping MCP3008. Then you can claim yourself a junior SPI ninja, 🙂
tlfong01

tlfong01
2427
yst 13:02
user image
Now I have uploaded your wiring and my pinout to compare.
Everything looks so far so good.
3 hours later…
tlfong01

tlfong01
2427
yst 16:22
Update using multi-meter to measure CS pin voltages. Earlier I mentioned that you can use multi-meter to measure the average/rms voltage of the CS pin which toggles between Low level (when select device) and High level when not selecting device. The more precise voltage depends on how frequent you repeat the chip select device.
So if you don’t select any device, CS should measure around Vcc of 3.3V. If you repeat select device, CS pin level would drop to Low level more if more frequently selecting devices. I experiment with repeatedly sent one byte (as in my loopback back test demo functions), with 1mS/10mS pause between selection, CS as measured by multimeter drops to about 2.7V. Anyway, if CS is around Vcc 3.3V, and drops to below 3V when repeated sending bytes, then it is very likely that CC is working OK.
To conclude, if loopback OK, then CLK, MOSI, MISO are OK. And if CS when not repeatedly sending bytes and repeatedly send bytes, multi-meter measured voltage is around 3.3V and below 3V down to 2.7V, then CS should also be working OK.
1 hour later…
tlfong01

tlfong01
2427
yst 17:52
Now let us see how to pink the one. First we, need to read the datasheet, and focus at the SPI write read bytes details. The following is the most critical picture.
user image
You need to read the datasheet a couple of times, and perhaps also read the discussions on MCP3008 in ths forum: (1) Datesheet – cdn-shop.adafruit.com/…, (2) Rpi Reading MCP3008 10 bit ADC Result Problem
raspberrypi.stackexchange.com/…
4 hours later…
tlfong01

tlfong01
2427
yst 22:04
Now I am thinking of “ping” as send/receive three bytes as below.
user image
Let me see, the first send byte is 0x01, second send byte = sing/diff d0 d1 d2 xxxx.
First receive byte = ????? Null B9 B8, Second Receive Byte = B7 to B0.
tlfong01

tlfong01
2427
yst 22:21
Now I am going to modify the loopbackThreeBytes function to pingMcp3008 function, as below.
user image
In other words, first second send byte = start bit and sin/dif d0, d1, and d2.
Second and third receive byte contains 10 bit ADC results.
2 hours later…
Aubrey Champagne
Aubrey Champagne
0:35
Thank you so much for the help with understanding this chip
9 hours later…
Aubrey Champagne
Aubrey Champagne
9:07
I haven’t been able to really digest what you have written just yet, but I did completly reflash, and start over the tutorial from the beginning. Still nothin. I am using raspian buster with all the recommended libraries. I have no idea whats going on, unless we got a bad batch of chips
tlfong01

tlfong01
2427
9:38
No problem. Let me see what you have been doing. (1) You successfully completed the SPI loopback test. (2) Next test is to use a multi-meter to test CS0 when repeatedly send out bytes, using the plug and play function also in my loopback module. (3) Next test is the ping MCP3008 test. If you understand what is going on in test (2), then you can try to following the design of my ping MCP3008 test.
If you are not trying to upgrade yourself to higher ninja levels, you can skip my design notes and wait sometime for me to finish the ping program and just plug and play the ping program to verify if you indeed got a batch of bad chips. But as I hinted earlier, the chance of getting a batch bad chips is much smaller then getting a batch of impatient and lazy newbies. Anyway, have a nice new year.
Ah, I forgot to ask two questions: (1) Do you know Ohm’s Law, (2) Do you know how to use a multi-meter?
2 hours later…
Aubrey Champagne

Aubrey Champagne
550
11:22
I do know Ohms law, and I do know how to use a multimeter.
Something that I thought was curious was this. I connected an LED to the analog in pin (channel 0), then connected the other leg to 3.3v then to ground. Both of those configurations didn’t make the LED light up. I would think that if the chip was accepting a signal, then the led would light up, but nothing happened. I don’t know what to do with this information, but I feel like I am not allowing the pin to accept a signal somehow
Looking at the datasheet, and the specs for the chip select pin, It seems like that pin is high when it is low, and low when it should be high. I think you had mentioned a test for that above that i will look back on.
Aubrey Champagne
Aubrey Champagne
11:59
This is a dumb question, but where can I get your loopback code? I have my multimeter ready.
1 hour later…
tlfong01

tlfong01
2427
13:08
I though you have read my suggestion below and tried it!
“For spi loopback tests in Rpi python 3.5x and 3.7x, you can search this forum using key words “SPI”, “loopback”. If you add the search word “tlfong01” you might find a couple of my answers with loopback python test programs, colourful waveforms illustrating the CLK, MOIS, MISO, CSn waveforms. You don’t need to understand the details of the programs. My programs are “plug and play/run” no library is required. Though you need to “uncomment” to select slight variation of loopback, say one, two, or three characters, or control characters…

(see full text)

tlfong01

tlfong01
2427
13:40
You need to study carefully the loopback three bytes test, because later you will find that if you replace the three input MOSI bytes by MCP3008 “Start ADC operation”, “Single/Differential Mode, Channel number” and third byte just dummy byte, then the second and third of the three bytes return at MISO contains the 11 bit ADC results. Never mind if you don’t know what actually I am talking about, because I am going to show the real ping MCP3008 function, with the input parameters in detail.
So after you tried the 3 byte loopback OK, you can then sit back and watch me how to ping MCP30008. Actually this this the second last step to read MCP3008. So you are almost there!
1 hour later…
tlfong01

tlfong01
2427
15:02
One more thing. I have not checked your program if it is using single or differential mode. Either case you need to pull the free input pins to ground, otherwise the dangling input pins might float and become higher or lower to the input pin you are using. The result is that the output will be zeros. See explanation below.
user image
Also important is the I2C speed cannot be too high or too low. If too high, then MCP3008 might not have time to charge up the sampling capacitor. If frequency too low, then the charge in the sampling cap will bleed or leak away, causing results too low, but should not to zero. I once tried very low frequency and found results unreliable.
tlfong01

tlfong01
2427
15:48
Now the time has come to write the ping MCP3008 function, based on the SPI loopback three bytes function which has already been tested (together with the multi-meter CS check when repeatedly write one byte). The only things remaining is to decide the first byte with the start ADC bit, and the single/diff/D2/D2/D0 byte.
So I go reading the MCP3008 datasheet for details. Then I had pleasant surprise, I found the MCP3008 config bits is identical to that of MCP3208. In other words, the same ping function can be used for both MCP3008 and MCP3208. Actually MCP3208 hardware architecture is almost the same as MCP3008, except the resolution is 12 bit instead of 10 bit, as can be compared with the pictures below.
user image
user image
Now I am using the simplest test config: (single channel, channel number 0). In other words S/D D2/D1/D0 = 0b1000. The only difference with MCP3008 is that the four config bits for MCP3208 is shifted two bits to the left. So far so good, ah, time for delayed afternoon coffee break, then jogging time. 🙂
6 hours later…
tlfong01

tlfong01
2427
22:23
Now I have drawn a schematic for testing both MCP3008 and MCP3208.
user image
Next step is writing the Ping MCP3x08 function.
tlfong01
send upload…
.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s