1

I am trying to get negative ADC value by giving -2.5 v to AD/DA high precision expansion board with Raspberry Pi 4. I am using python programming with twos complement of buffer size 200, unfortunately, I am not getting negative ADC value properly. as per condition if I had applied -2.5v I will get values @ -2.53456 form but I am getting around -0.5465 which is not correct.

Anybody help me to find a negative value. if someone did work on the negative value before or now doing.

Thanks in advance

tlfong01
  • 4,665
  • 3
  • 10
  • 24
  • 1
    No evidence that this question has anything to do with the Pi. – joan Feb 25 '20 at 14:35
  • Ah, I guess you are using ADS1250. The offset setting is a bit tricky. You need to read the datasheet very carefully not to go wrong. References: (1) https://raspberrypi.stackexchange.com/questions/99353/separate-spi-data, (2) https://raspberrypi.stackexchange.com/questions/95843/waveshare-adc-board-negative-value-issue. – tlfong01 Feb 25 '20 at 14:35
  • 1
    i am using ADS1256. could you know how to get negative value. any sample program do you have in python or c – Amritpal singh sodhi Feb 25 '20 at 15:31
  • Ref 1 describes ADS1256 board, wiring, python SPI set up functions. You can follow up and describe your spec, init, setup, config, convert, read, and print results functions. If you have a spec, perhaps somebody might help writing the complete program for you. If you has a program spec and your already written, documented, but buggy program listing, perhaps somebody can debug the program for you. PS - Ref 2 points out it is important to set the "Vrefp" and "Vrefn", "PGA". If you don't set them correctly, you result won't offset and scale correctly. – tlfong01 Feb 26 '20 at 06:33
  • So if your don't have a complete program listing, perhaps you can just list the functions doing Vrefp, VrefN, and PGA etc. – tlfong01 Feb 26 '20 at 06:36
  • You other functions might also be buggy and give you wrong results. For example, you mentioned that you have a 200 byte two's complement result buffer. I am not sure if you are doing continuous conversions. For debugging, it is always a good idea to do one off or one short testing. So for 24 bit 2's complement results, your final result is 24 bit = 3 bytes. Then you need a conversion function to convert 24 bit 2's complement to signed decimal. Now you need to first verify your function is working properly. I once used such a 2's compliment to signed decimal and I did first test before use. – tlfong01 Feb 26 '20 at 07:11
  • My 2'c complement to signed decimal is used in my SPI ADXL345 accelerometer. You my see how I used this universal converter in this and otther applications. Let me see if I can show you the program listing. – tlfong01 Feb 26 '20 at 07:14
  • Now let me show you my multibyte 2's complement to signed decimal function and testing results: (1) https://penzu.com/p/f5968e88. (The relevant functions are hightlighted in bright red.) You may also like to read the complete listing of my plug and play SPI ADXL345 accelerometer test program, with functions of config, analog to decimal convert, binary to decimal convert, debug print functions. So if you show me the config part of your ADS1256 program, then I can check out how you do positive and negative reference and gain setting. Cheers. – tlfong01 Feb 26 '20 at 07:54
  • 1
    Thanks for your help. please find attach link. I marked it in red where i did twos complement and reading data. https://penzu.com/p/f38ec540. – Amritpal singh sodhi Feb 26 '20 at 09:21
  • @Amritpal singh sodhi, Many thanks for your structured, and very well self documenting program. I need to read the ADS1256 datasheet to refresh my memory, before walking through your program. I am a slow learner, and my toy playing list is a bit long, so it it might take me a couple of days. Cheers. – tlfong01 Feb 26 '20 at 09:47
  • Anyway, let us look at the read your function of "ADS1256 results and convert to decimal". It is too messy to list the python statements as a comments, and I don't know how to open a chat room to upload an image, so I upload the function listing as part of my answer. – tlfong01 Feb 29 '20 at 03:51
  • Now I think the green and pink part (10 lines,165 to 175) look OK. What we need to verify is the blue and yellow part (9 lines, 176 to 185). As I mentioned earlier, my similar conversion is a verified function with the following two parameters (1) Two's compliment string of variable number of bytes, (2) string length in number of bytes. I have two suggestions (a) You first verified my function, as described above, and replace your 9 python statements by my function, and see if you can get negative and to the scale values, if not, then we need to debug a level up, / to continue, ... – tlfong01 Feb 29 '20 at 04:06
  • (b) You explain what is going on in your 9 blue and yellow statements, using pseudo code or comments, so everybody can help debugging. Comments and counter suggestions welcome. Cheers. – tlfong01 Feb 29 '20 at 04:07
  • Now I have listed my conversion function to compare and contrast with yours. You see that my verified conversion is in the form of a function: "convertTwoComNumToDecNum()", so later when I need to convert any times, for any length of binary two's compliment string, I just call this function (the dark blue parts), NOT repeating the messy error prone statements, because they are info hidden, or ADT treated. – tlfong01 Feb 29 '20 at 04:28
  • And as you can see in my very long penzu lab log, I have many debug print statements when troubleshoots the low and high level functions. The debug print statements are commented out when testing is complete. I also saved sample outputs for any later debugging, in case my programs gets more and more complex. – tlfong01 Feb 29 '20 at 04:45
  • Anyway, if you agree my suggestion to troubleshoot your convert-2's-comp-to- sign-decimal function, then we can move up one level and test your ADS1256 class. I have summarized your class in Appendix C. My first impression is that it is a bit "Hard wired", therefore not easy or flexible enough to try some methods with different parameters such as gain factor etc to compare and contrast the results. It would be nice if you can give us the main test section, where you create an object of the class, and how you call various methods and different parameter to do the tests, and with sample output? – tlfong01 Feb 29 '20 at 09:17

2 Answers2

1

I think the easiest way is to put the signal through a resistor divider attached to 5V (or whatever your ADC reference is): a 2x divider will let you measure voltages from +5V to -5V:

schematic

simulate this circuit – Schematic created using CircuitLab

The conversion to real voltage is linear: 0V measured by the ADC corresponds to -5V on the signal wire, 5V measurement corresponds to 5V signal value and 2.5V measurement corresponds to 0V signal value. In terms of software, that's V_real = 2*V_meas - 5*K_1Volt, where K_1Volt is the numeric value corresponding to 1V.

Obviously, your measurement resolution w.r.t the real signal value will be twice as bad, and your signal source should support a 20kOhm load for this to work properly.

Dmitry Grigoryev
  • 27,928
  • 6
  • 53
  • 144
  • I think your simple resistor voltage divider works. To overcome the low input impedance problem, we can use an OP Amp as a digital buffer. Or we can use a dual+-5V power supply op amp and shift up input signal by 2.5V. Just brainstorming. – tlfong01 Feb 27 '20 at 08:56
  • 1
    Ok Thanks @Dmitry Grigoryev,tlfong01 : I am doing simple register voltage divider i got now posItive measured value but how i program the formula V_real = 2V_meas - 5K_1Volt, in python. I gave the link of ADS 1256 file could you let me know .its library where i did twos complement .it mark in red.penzu.com/p/f38ec540. – Amritpal singh sodhi Feb 27 '20 at 13:04
  • Oh my goodness. So you are not using #Dmitry's classic two register voltage divider method. Instead, you are using a "two register" divider approach, which I found wierd. As I said earlier, my accelerometer output is 2's complement covering postive and negative values. I am using 2's compliment converting to signed decimal function I borrowed form StackOverflow or elsewhere I forgot. As almost always, I did verify if the convering function is working in ALL conditions, no matter the number of bytes of the original 2's compliment string. / to continue, ... – tlfong01 Feb 29 '20 at 02:34
  • I input pos and neg values, middle and boundaries, one to three bytes long, compare the function output with an online 2C to decimal converter. I found everything OK, before I decided to use it. My penzu program listing contains my conversion function with sample output. So you can use python to input different values to my converter and print out the results to verify. I have not yet looked at your do analog to digital conversion and convert 2C to decimal funtion, because you combined two things in one function, and I don't wish to spend time splitting it and separate test one of them. – tlfong01 Feb 29 '20 at 02:35
  • Actually I did rerfresh my memory on 2's compliment represention and did mental checking using my studpid brain, with questions like below, on 8 bits only. (1) How can 8 bits donote +3? OK, it 00000011, so my function should give "+" and 00000011. (2) How to denote -4 then? Well, I know the mechanical procedure is first find one'c complement, then add one. Of course I cheated and asked help on line (https://www.exploringbinary.com/twos-complement-converter/). / to continue, ... – tlfong01 Feb 29 '20 at 02:35
  • Anyway I used the online converter to verify my python converter function. Now of course you can verify if my converter is correct, AND IF YOUR TWO REGISTER APPROACH IS ALSO CORRECT (I doubt it). Sorry for my blunt comments, but you are doing 24 bit high precision perhaps life critical medical or military applications, you need to verify every step of your design. I won't be so strict/severe if you are doing Micky Mouse hobbyist projects using the amateurish MCP3008. – tlfong01 Feb 29 '20 at 02:38
  • 1
    Thanks all for your suggestion. currently i am using two register voltage divider method but i am not getting properly output. The problem is that the ADS has a small input capacity and together with the resistors i build an RC time constant that causes the delay in its falling edge. If i decrease the value of the resistors, i can solve this problem, but then the current becomes too large, so that i have too many losses in the resistors of the voltage sources.One solution is to use an operational amplifier. I want to know which IC i can use for summing non inverting amplifier. – Amritpal singh sodhi Mar 04 '20 at 11:22
  • 1
    @Amritpalsinghsodhi Try adding small capacitors (20 pF?) in parallel with resistors. The amplifiers also have input capacitance, so you'll have a similar problem if you use one. If you really need fast measurements, get an ADC which can cover your entire voltage range. – Dmitry Grigoryev Mar 04 '20 at 11:37
  • 1
    @Dmitry Grigoryev, I tired by adding capacitor in parallel with resistors but it did not improve signal, same delay in its falling edge. – Amritpal singh sodhi Mar 04 '20 at 17:40
0

Answer

Discussion

Let us first check out the ADS1256' "Read 2's compliment results and convert to signed decimal" function.

Update 2020mar02hkt1905

If you have tested you 2's complement to signed decimal converter OK, you can then define the converter as a function, as shown in my ADXL345 demo program, to be used later. This converter function should not be defined in you ASD1256 class, and of course should not be merged with the convert analog to digital function, making debugging difficult.

Now let me suggest how to start testing your ADS1256 class. I found that you just defined the class, but have not shown how to instantiate an ADS1256 object and do the conversion, print the results etc. I usually suggest to do the "ping" thing, ie, read the device ID. In my ADXL345 demo program, you can see that my ping function is to read the device id "0xe5", I have other ping functions for other devices.

I found that your ADS1256 class has a similar ping or read device ID function, as listed below.

def ADS1256_ReadChipID(self):
    self.ADS1256_WaitDRDY()
    id = self.ADS1256_Read_data(REG_E['REG_STATUS'])
    id = id[0] >> 4
    # print 'ID',id
    return id

So my suggestion to start debugging is to do the following:

Write a very simple program to read device ID.

It is only when you have successfully

(1) read the device ID, then you can do

(2) the AD conversion, and

(3) convert to signed decimal and (4) print results.

My ADXL345 demo program is a plug and play self contained program without need of other libraries. If you have ADXL345, then you just run my demo program to get the results.

Then you can translate my program for your ADS1256. There is of course the read negative value, and scaling offset problem not yet solved.

Now I would suggest to solve the problem is two steps,

(a) read a standard positive value, say +2.00V, to make sure your scaling and gain is correctly set.,

(b) then set scaling and gain for negative -2.00V.

If your step (a) is debugged, then you are almost there. You can do trials and errors to do setting for negative values.

If you can show me the program listing and sample results like my ADXL345 demo program, I am happy to follow up and make more suggestions or other thing, like how to reduce noises etc.


References

(1) ADS1256 24-bit Analog to Digital Converter Datasheet - TI


Appendices

Appendix A - ADS1256 Read three byte long 2's compliment results and convert to signed decimal

ads1256 results


Appendix B - ADXL345 Read three two-byte-long 2's compliment results and convert them to signed decimal

Troubleshooting Tips - Insert break points, fake values, and debugging print statements statements in the pink and green sections.

adxl345 1


Appendix C - ADS1256 Class

Comment: The class summarized below seems not flexible enough toe test the ADS1256 ADC device. It would be nice to include a sample instantiation and methods used to change the parameters such as gain, data rate (or one shot) with sample outputs, to clarify how a couple of tests have been made.

ads1256 class


tlfong01
  • 4,665
  • 3
  • 10
  • 24