ADS1256 programming notes

Active today
Viewed 50 times

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

 New contributor
  • 1
    No evidence that this question has anything to do with the Pi. – joan Feb 25 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) raspberrypi.stackexchange.com/questions/99353/separate-spi-data, (2) raspberrypi.stackexchange.com/questions/95843/…. – tlfong01 Feb 25 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 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 at 6: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 at 6: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 at 7: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 at 7:14   
  • Now let me show you my multibyte 2’s complement to signed decimal function and testing results: (1) 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 at 7:54   
  • 1
    Thanks for your help. please find attach link. I marked it in red where i did twos complement and reading data. penzu.com/p/f38ec540. – Amritpal singh sodhi 2 days ago
  • @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 2 days ago   
  • 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 5 hours ago   
  • 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 5 hours ago   
  • (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 5 hours ago    
  • 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 4 hours ago   
  • 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 4 hours ago   
  • 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 35 secs ago   Edit   

2 Answers




Let us first check out the ADS1256′ “Read 2’s compliment results and convert to signed decimal” function.


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

Note: Your class summarized below seems not flexible, therefore not easily to test. It would be nice to include a sample instantiation and which methods are then used to change the parameters such as gain, data rate (or one shot) with sample outputs, to clarified how a couple of tests have been made.

ads1256 class


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:


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.

  • 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 2 days ago    
  • 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 = 2*V_meas – 5*K_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 yesterday
  • 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 6 hours ago   
  • 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 6 hours ago   
  • 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 (exploringbinary.com/twos-complement-converter). / to continue, … – tlfong01 6 hours ago   
  • 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 6 hours ago   

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 )

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: