cyrilz
Posts: 5
Joined: Wed Apr 24, 2013 11:47 am

Gertboard AD converter gives wrong value (solved)

Wed Apr 24, 2013 3:30 pm

Hello all,

I've just bought a PI b and a Gertboard and I'm very satisfied of both. But when I come to try dad.py script, output seems wrong.

When I write 255 in the D/A converter, I read a value of 951 instead of 640 as shown in sample scripts.

I've mesured 2,064V at the DA1 pin, so what can be wrong here ?

Note : the python function given with the board is :

Code: Select all

def get_adc(channel):                                   # read SPI data from MCP3002 chip
    spi.open(0,0)                                       # Gertboard ADC is on SPI channel 0 (CE0 - aka GPIO8)
    if ((channel > 1) or (channel < 0)):                # Only 2 channels 0 and 1 else return -1
        return -1
    r = spi.xfer2([1,(2+channel)<<6,0])     # these two lines are an spi driver for the ADC, they send three bytes
    ret = ((r[1]&31) << 6) + (r[2] >> 2)    # and receive back three. This line extracts the 0-1023 ADC reading
    return ret 
I use channel 0 since I've put a direct jumper between DA1 and AD0. the code is assuming the 10 bit-data is located as follow :

Code: Select all

XXXX XXXX   XXXX NNNN   NNNN NNXX
byte 0      byte 1      byte 2   
Thanks in advance

Cyril

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard AD converter gives wrong value

Wed Apr 24, 2013 4:18 pm

How are you writing the data to the DAC?
Note this was written for the MCP4802, if your DAC is different it may not work.

From memory (it's all in the datasheet) the DAC driver works by writing two bytes to the DAC.
The makeup of those bytes can be found in line 24 of the dad.py program.

Code: Select all

whole_list = [dac_channel,0,gain,1,bin_data_bits,'0000']    # assemble whole 16 bit string in a list
bit 1 is the channel, bit 2 is 0, bit 3 is the gain, bit 4 is 1, then 8 data bits and four trailing zeroes which are ignored by the DAC.

If you just send 255 to the DAC, it's not surprising it doesn't work because that's just one byte all 1s. There are all sorts of other settings as well and the DAC needs two bytes. You need to have a good look at the data sheet. I had to do that when I wrote the DAC driver (so no sympathy :mrgreen: ). It was a big stretch for me at the time and I was amazed it worked at all. But it does - even if it's not very elegantly coded. It works, and that's what matters to me. You're only the second person in >3k downloads to even query it.

Very likely it can be improved and made more understandable, but I've got too much on now.
Alex Eames RasPi.TV, RasP.iO

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard AD converter gives wrong value

Wed Apr 24, 2013 4:38 pm

Having said all that, I've just reread your post and it looks as if you're getting the right voltage on the DAC, so, next question is.

What chip is your ADC? The driver was written for an MCP3002, if you have something else (e.g. 3202) it may need a tweak.
Alex Eames RasPi.TV, RasP.iO

cyrilz
Posts: 5
Joined: Wed Apr 24, 2013 11:47 am

Re: Gertboard AD converter gives wrong value

Wed Apr 24, 2013 4:45 pm

Thanks for your answer.

My DAC is a 4802 (8-bit) and my ADC is a 3002, so I guess I'm right when I simply copy dac_write function from dad.py, (same as your example).

Since I mesured 2,06V at pin DA1 i guess the DAC is OK (almost correct regarding measuring errors), I guess my reading function is wrong, but I also copied get_adc function from dad.py (see my post).

Can my ADC be dead ? But in this case I would have any reading. I've 0 when I write 0 and 951 when I write 255...

EDIT : Plus when I put 3,3V on AD1 I read 1023.

Cyril

cyrilz
Posts: 5
Joined: Wed Apr 24, 2013 11:47 am

Re: Gertboard AD converter gives wrong value

Wed Apr 24, 2013 5:08 pm

OK,

I should have read (and understood) the J7 post in this forum.
Now I read 631 (almost OK) ...

Don't understand why but it works now, so forget about my post :?

Thanks for your help

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard AD converter gives wrong value

Wed Apr 24, 2013 5:29 pm

cyrilz wrote:OK,

I should have read (and understood) the J7 post in this forum.
Now I read 631 (almost OK) ...

Don't understand why but it works now, so forget about my post :?

Thanks for your help
That jumper ensures the Gertboard chips are powered adequately (from the Pi's 5V rail). If not attached, the GB is trying to draw power from the 3V3 rail, which really doesn't have enough spare capacity to power the chips on the Gertboard (as you have proven).

I think I'm going to start all future answers in the GB forum with
"You have installed J7 jumper and put your Gertboard on the right way haven't you?" :)

Well at least you got me to explain a little about how the DAC driver works. I had to look at the code to remember. ;)
Alex Eames RasPi.TV, RasP.iO

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Gertboard AD converter gives wrong value (solved)

Thu Apr 25, 2013 2:59 pm

If you wnt to test quickly without writing a program, then you can install wiringPi.

Connect DA0 to AD0, then

gpio gbw 0 0
gpio gbr 0 # Expect 0
gpio gbw 0 128
gpio gbr 0 # Expect 332
gpio gbw 0 255
gpio gbr 0 # Expect 664.

See: https://projects.drogon.net/raspberry-p ... i-devices/ for a few more details.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Gertboard AD converter gives wrong value (solved)

Thu Apr 25, 2013 3:24 pm

[email protected] wrote:If you wnt to test quickly without writing a program, then you can install wiringPi.

Connect DA0 to AD0, then

gpio gbw 0 0
gpio gbr 0 # Expect 0
gpio gbw 0 128
gpio gbr 0 # Expect 332
gpio gbw 0 255
gpio gbr 0 # Expect 664.

See: https://projects.drogon.net/raspberry-p ... i-devices/ for a few more details.

-Gordon
Looks like you're doing some brilliant stuff with WiringPi Gordon. :D
I was hoping to try out some of the WP2 more advanced features in the Python variant, but people keep asking me to do stuff. :lol:
Alex Eames RasPi.TV, RasP.iO

Return to “HATs and other add-ons”