User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

ADC Differential Non-Linearity

Sat Jan 23, 2021 2:54 pm

With little info available yet about the ADC, I thought I would write some code to measure its differential non-linearity - ie whether there are missing or near missing codes when a signal is swept through the range. I wrote a MicroPython program to toggle an I/O bit and pass it through a long time constant RC filter (series 100kohm and 10uF to 0V) to the ADC2 input. I integer divided the ADC reading by 16 to get back to the original 12-bit ADC readings, and toggled the output bit when the ADC reading got to 4000 and 100. I put all the readings in a 4096 word histogram array and created an output file when a few histogram readings sampled from the middle reached 1000 or any other value that 'maxh' is set to . The resultant histogram was graphed in Excel, and shows here 4 strange peaks. The ends of the histogram are higher than the middle because of the exponential nature of the waveform as the capacitor charges and discharges. Closer examination of the output file shows lots of other anomalies. I have a zoomed in view at the low end. People might to like to play with the code here, change the timing etc. Currently running with no sleep command. I've put the csv file from the program between code brackets to give a scrolling list in case anyone wants to peer at the numbers in detail.
hist1.png
hist1.png (114.53 KiB) Viewed 2916 times
Hist_2 cropped.png
Hist_2 cropped.png (50.07 KiB) Viewed 2916 times

Code: Select all

 # test ADC with histogramming
 # Tony Abbey 20210123
import machine
import utime

ADC2 = machine.ADC(28)
LED = machine.Pin(25, machine.Pin.OUT)
OUTPUT = machine.Pin(22, machine.Pin.OUT)
hist = [0] * 4096  # create array and fill with zeroes
up = True
inc = 1
hflag = False
end = False
maxh = 1000
while not end:

    ADC = ADC2.read_u16()//16
    if up is True:
        if ADC > 4000:
            up = False
            hflag = True
        inc = 1
    elif up is False:
        if ADC < 100:
            up = True
        inc = 0
    LED.value(inc)
    OUTPUT.value(inc)
#     print(ADC)
    hist[ADC] += 1
#    utime.sleep(0.0001)
    if hflag is True:
       for i in range(2000,2009):
         j= hist[i]
         print(i-2000,j)
         if j > maxh:
             end = True
       hflag = False

file = open("hist.txt", "w")
file.write(str(hist))
file.flush()
file.close
 

Code: Select all

 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 29, 623, 3406, 5236, 1505, 5442, 5557, 5426, 5243, 5176, 5277, 5009, 2097, 5008, 5028, 4926, 4886, 4658, 4800, 4651, 1293, 4616, 4644, 4489, 4502, 4334, 4436, 4212, 911, 4348, 4314, 4192, 4120, 4083, 4074, 3973, 1168, 4016, 4027, 3818, 3941, 3744, 3907, 3628, 1596, 3744, 3702, 3706, 3654, 3497, 3633, 3460, 1021, 3472, 3518, 3401, 3500, 3236, 3459, 3374, 1268, 3289, 3388, 3170, 3209, 3146, 3245, 3108, 847, 3089, 3127, 3099, 3047, 2984, 3087, 2914, 1259, 3002, 2938, 2961, 2945, 2760, 2927, 2797, 772, 2813, 2817, 2742, 2820, 2659, 2844, 2618, 1253, 2678, 2721, 2602, 2613, 2523, 2763, 2456, 728, 2634, 2566, 2515, 2495, 2478, 2502, 2518, 1055, 2419, 2532, 2389, 2449, 2332, 2456, 2301, 730, 2294, 2436, 2318, 2343, 2267, 2371, 2198, 956, 2213, 2311, 2204, 2220, 2207, 2270, 2146, 566, 2212, 2292, 2115, 2141, 2084, 2221, 2088, 815, 2138, 2163, 2086, 2075, 2019, 2090, 2018, 575, 2009, 2064, 1992, 2040, 1935, 2076, 1784, 201, 2007, 2039, 1957, 1933, 1924, 1920, 1867, 530, 1899, 1947, 1874, 1907, 1833, 1925, 1856, 766, 1879, 1847, 1834, 1791, 1793, 1887, 1748, 492, 1809, 1799, 1796, 1771, 1713, 1770, 1784, 685, 1741, 1783, 1699, 1719, 1694, 1728, 1648, 470, 1747, 1739, 1652, 1674, 1620, 1695, 1683, 682, 1677, 1634, 1703, 1565, 1607, 1669, 1559, 435, 1626, 1619, 1559, 1576, 1582, 1643, 1566, 643, 1587, 1566, 1569, 1531, 1509, 1604, 1441, 437, 1522, 1568, 1543, 1529, 1410, 1597, 1457, 609, 1477, 1588, 1396, 1499, 1437, 1519, 1373, 414, 1454, 1529, 1417, 1425, 1398, 1462, 1436, 578, 1430, 1393, 1441, 1368, 1397, 1420, 1389, 398, 1361, 1447, 1331, 1410, 1334, 1383, 1367, 571, 1385, 1371, 1324, 1344, 1350, 1341, 1319, 365, 1353, 1331, 1308, 1313, 1306, 1350, 1269, 309, 1341, 1323, 1281, 1298, 1233, 1310, 1270, 350, 1323, 1242, 1302, 1235, 1265, 1291, 1239, 543, 1237, 1245, 1263, 1219, 1264, 1233, 1218, 331, 1233, 1288, 1172, 1205, 1205, 1222, 1196, 499, 1213, 1221, 1180, 1208, 1206, 1197, 1173, 312, 1185, 1204, 1160, 1173, 1171, 1160, 1180, 507, 1145, 1208, 1132, 1174, 1106, 1199, 1112, 287, 1169, 1157, 1133, 1144, 1110, 1126, 1144, 483, 1105, 1189, 1090, 1130, 1095, 1122, 1086, 312, 1104, 1115, 1090, 1103, 1040, 1178, 1035, 446, 1129, 1110, 1076, 1042, 1087, 1077, 1057, 296, 1081, 1086, 1039, 1076, 1038, 1072, 1077, 425, 1015, 1054, 1056, 1063, 1005, 1058, 1018, 293, 1068, 1007, 1032, 1076, 973, 1066, 991, 422, 1053, 1004, 999, 1038, 1004, 1025, 965, 283, 1020, 1006, 1001, 1025, 958, 1031, 1001, 8966, 1022, 1019, 938, 964, 955, 1002, 934, 244, 951, 976, 962, 957, 954, 966, 896, 407, 968, 959, 945, 928, 948, 954, 904, 240, 991, 958, 908, 897, 945, 926, 910, 330, 963, 927, 936, 893, 919, 911, 927, 220, 904, 935, 889, 927, 895, 899, 915, 365, 888, 921, 890, 864, 905, 893, 878, 249, 914, 903, 845, 916, 860, 888, 864, 379, 915, 879, 882, 857, 832, 912, 848, 218, 892, 860, 892, 874, 805, 889, 846, 317, 903, 851, 859, 816, 837, 910, 817, 214, 825, 907, 835, 852, 793, 886, 829, 324, 840, 816, 852, 837, 787, 839, 833, 217, 877, 808, 832, 802, 822, 853, 777, 344, 827, 815, 832, 833, 790, 832, 777, 211, 819, 841, 803, 794, 777, 825, 797, 147, 832, 800, 791, 781, 803, 783, 826, 210, 777, 810, 776, 777, 800, 794, 757, 341, 794, 780, 799, 742, 830, 767, 758, 192, 812, 758, 744, 783, 761, 811, 737, 304, 745, 861, 713, 764, 743, 760, 748, 222, 766, 811, 713, 766, 710, 796, 751, 315, 738, 765, 744, 755, 725, 784, 716, 211, 741, 775, 725, 759, 694, 769, 718, 312, 777, 731, 743, 730, 735, 764, 706, 204, 714, 744, 722, 734, 724, 740, 692, 321, 710, 748, 729, 715, 698, 751, 683, 205, 757, 733, 680, 723, 674, 730, 714, 272, 714, 712, 730, 694, 720, 721, 684, 192, 681, 722, 729, 675, 692, 727, 710, 289, 673, 715, 695, 733, 653, 712, 690, 181, 689, 689, 706, 690, 670, 691, 613, 88, 725, 689, 679, 700, 666, 711, 673, 174, 695, 678, 670, 661, 667, 727, 662, 259, 685, 698, 687, 655, 656, 698, 649, 181, 686, 668, 673, 677, 639, 677, 662, 260, 682, 671, 655, 656, 661, 713, 631, 147, 663, 678, 658, 642, 672, 658, 635, 277, 669, 684, 631, 633, 632, 674, 654, 187, 635, 662, 651, 646, 614, 691, 633, 257, 664, 659, 650, 648, 630, 652, 619, 163, 670, 630, 661, 606, 632, 661, 605, 272, 641, 659, 622, 609, 658, 636, 616, 152, 644, 656, 627, 613, 599, 677, 593, 265, 621, 629, 622, 616, 590, 675, 595, 162, 645, 633, 638, 606, 616, 635, 599, 270, 603, 633, 609, 607, 609, 644, 602, 130, 610, 660, 598, 592, 593, 649, 604, 115, 622, 608, 604, 629, 610, 607, 585, 145, 637, 617, 601, 593, 586, 625, 583, 253, 611, 609, 615, 590, 591, 629, 540, 153, 643, 611, 555, 614, 587, 600, 602, 222, 599, 607, 600, 583, 564, 617, 566, 154, 611, 598, 589, 573, 605, 593, 585, 215, 598, 617, 569, 596, 564, 594, 567, 157, 600, 557, 597, 564, 580, 593, 565, 253, 615, 579, 584, 544, 598, 589, 558, 154, 563, 599, 548, 578, 571, 591, 572, 243, 577, 592, 543, 602, 526, 572, 576, 154, 574, 590, 567, 551, 527, 598, 547, 237, 581, 554, 548, 560, 599, 557, 550, 134, 581, 581, 542, 564, 540, 590, 570, 230, 566, 529, 573, 551, 521, 591, 549, 147, 564, 570, 548, 569, 537, 572, 538, 161, 628, 577, 531, 547, 582, 528, 545, 134, 560, 562, 536, 552, 555, 547, 540, 212, 593, 544, 521, 564, 542, 549, 506, 132, 559, 558, 557, 537, 536, 554, 554, 195, 541, 554, 519, 528, 540, 547, 530, 138, 565, 544, 531, 525, 547, 555, 525, 215, 538, 555, 520, 537, 526, 544, 522, 139, 541, 523, 540, 527, 520, 558, 501, 195, 592, 536, 482, 557, 527, 542, 512, 163, 504, 559, 522, 513, 529, 508, 529, 234, 523, 528, 531, 529, 518, 510, 518, 126, 542, 533, 522, 535, 505, 531, 493, 198, 566, 499, 514, 500, 534, 539, 495, 120, 554, 521, 499, 519, 509, 544, 487, 215, 500, 527, 548, 500, 505, 543, 500, 141, 499, 523, 489, 532, 505, 537, 487, 77, 540, 506, 515, 529, 501, 515, 465, 139, 528, 526, 501, 551, 468, 509, 511, 185, 523, 517, 521, 487, 494, 530, 459, 153, 537, 498, 497, 487, 494, 513, 498, 192, 535, 478, 504, 502, 498, 516, 462, 140, 526, 506, 499, 480, 506, 513, 482, 200, 527, 509, 440, 528, 510, 511, 483, 118, 493, 521, 486, 463, 522, 529, 483, 176, 514, 505, 491, 471, 505, 495, 485, 129, 479, 500, 543, 466, 463, 506, 477, 178, 530, 492, 489, 487, 474, 505, 464, 111, 538, 505, 482, 498, 462, 508, 468, 192, 476, 522, 443, 506, 468, 511, 456, 115, 503, 511, 461, 500, 478, 510, 453, 214, 465, 520, 456, 460, 494, 499, 455, 119, 476, 485, 518, 478, 448, 502, 442, 41, 512, 475, 457, 484, 474, 499, 463, 108, 499, 489, 479, 457, 477, 489, 471, 177, 485, 496, 477, 453, 462, 512, 474, 99, 487, 475, 481, 465, 475, 480, 473, 184, 467, 494, 442, 467, 478, 485, 463, 133, 473, 482, 462, 475, 441, 516, 442, 183, 474, 493, 455, 469, 476, 463, 453, 135, 489, 441, 492, 448, 466, 501, 462, 181, 477, 458, 469, 444, 477, 468, 464, 106, 510, 475, 450, 429, 476, 489, 445, 177, 483, 484, 435, 472, 455, 462, 483, 103, 480, 451, 457, 467, 464, 467, 423, 185, 491, 459, 468, 438, 457, 485, 460, 95, 475, 487, 426, 451, 465, 448, 469, 190, 471, 444, 468, 466, 451, 462, 438, 128, 470, 464, 453, 417, 471, 466, 445, 82, 495, 450, 460, 438, 456, 473, 427, 104, 490, 456, 452, 441, 457, 472, 448, 170, 474, 459, 458, 409, 487, 445, 464, 108, 438, 443, 465, 449, 452, 473, 446, 160, 438, 483, 442, 418, 464, 459, 457, 98, 469, 482, 435, 436, 463, 441, 425, 163, 440, 475, 453, 469, 412, 469, 455, 103, 446, 462, 430, 440, 462, 433, 446, 176, 498, 430, 437, 453, 459, 448, 428, 99, 449, 481, 424, 429, 420, 468, 453, 171, 455, 442, 457, 433, 430, 451, 447, 116, 443, 450, 434, 455, 407, 486, 431, 174, 425, 459, 438, 443, 419, 473, 405, 100, 477, 473, 403, 417, 436, 443, 457, 179, 441, 448, 421, 460, 442, 431, 428, 125, 443, 425, 443, 430, 417, 443, 445, 3775, 477, 465, 422, 419, 425, 468, 414, 88, 467, 454, 432, 422, 455, 407, 445, 157, 454, 437, 447, 411, 454, 433, 425, 102, 418, 464, 435, 425, 431, 460, 419, 159, 427, 470, 417, 424, 431, 435, 429, 103, 435, 459, 419, 428, 411, 435, 458, 170, 426, 451, 407, 434, 420, 453, 398, 109, 455, 440, 429, 414, 445, 424, 423, 163, 463, 447, 422, 412, 430, 439, 441, 118, 426, 415, 434, 440, 392, 438, 436, 154, 442, 453, 434, 415, 416, 434, 412, 102, 456, 426, 432, 416, 435, 432, 407, 163, 426, 454, 421, 407, 435, 423, 415, 100, 472, 411, 412, 432, 409, 435, 426, 187, 441, 410, 457, 405, 409, 432, 419, 93, 445, 440, 418, 417, 416, 427, 413, 83, 450, 447, 398, 420, 431, 419, 444, 112, 402, 422, 422, 428, 427, 424, 436, 156, 416, 423, 428, 427, 435, 397, 418, 104, 433, 441, 446, 377, 442, 417, 424, 150, 432, 449, 423, 391, 414, 421, 436, 108, 411, 451, 383, 432, 408, 424, 412, 180, 435, 423, 425, 445, 393, 400, 412, 106, 448, 408, 421, 424, 412, 445, 403, 150, 454, 407, 410, 438, 422, 439, 389, 97, 420, 435, 389, 428, 441, 419, 437, 150, 428, 413, 382, 447, 408, 434, 420, 98, 409, 415, 445, 411, 392, 417, 414, 176, 428, 436, 436, 374, 411, 415, 400, 115, 434, 426, 424, 404, 417, 404, 435, 166, 421, 446, 387, 405, 411, 448, 373, 108, 442, 400, 431, 421, 404, 435, 340, 33, 451, 425, 418, 442, 400, 415, 400, 110, 421, 409, 448, 394, 397, 419, 398, 159, 429, 422, 416, 405, 427, 407, 408, 110, 425, 442, 418, 377, 407, 421, 414, 140, 464, 433, 375, 402, 406, 446, 393, 109, 425, 416, 410, 414, 397, 412, 412, 170, 407, 439, 402, 407, 428, 409, 391, 105, 448, 410, 399, 413, 403, 419, 390, 147, 460, 415, 409, 403, 430, 419, 391, 97, 426, 439, 399, 413, 413, 445, 404, 135, 421, 431, 399, 398, 418, 432, 407, 95, 408, 434, 377, 410, 408, 421, 397, 169, 434, 425, 389, 373, 438, 409, 421, 103, 419, 419, 391, 418, 410, 420, 392, 164, 432, 416, 413, 368, 435, 422, 401, 85, 434, 404, 417, 425, 414, 393, 383, 70, 456, 438, 383, 416, 393, 417, 439, 96, 384, 441, 401, 406, 420, 420, 390, 152, 439, 406, 406, 394, 420, 426, 395, 107, 400, 432, 408, 373, 417, 430, 398, 159, 422, 431, 391, 409, 413, 430, 356, 84, 449, 412, 415, 411, 381, 440, 395, 174, 416, 412, 398, 413, 419, 413, 388, 97, 422, 392, 429, 409, 399, 424, 404, 152, 429, 424, 407, 391, 436, 416, 369, 82, 434, 427, 408, 396, 429, 409, 383, 165, 440, 427, 375, 425, 399, 435, 369, 98, 445, 403, 416, 384, 395, 429, 399, 163, 427, 399, 381, 437, 393, 441, 393, 98, 412, 416, 390, 401, 440, 399, 424, 141, 400, 406, 440, 404, 400, 406, 380, 104, 426, 435, 398, 395, 422, 280, 3, 0, 509, 408, 401, 396, 398, 431, 402, 89, 422, 431, 393, 410, 410, 399, 411, 157, 414, 414, 428, 413, 390, 399, 440, 100, 382, 431, 410, 380, 416, 421, 386, 141, 438, 423, 429, 367, 419, 409, 414, 93, 432, 402, 434, 390, 411, 421, 381, 162, 409, 417, 401, 408, 415, 446, 361, 94, 427, 419, 411, 396, 420, 423, 376, 127, 459, 403, 411, 421, 423, 437, 382, 87, 431, 413, 397, 407, 392, 422, 421, 157, 412, 449, 375, 402, 407, 455, 386, 88, 418, 404, 411, 391, 428, 417, 400, 135, 429, 392, 434, 409, 408, 416, 407, 87, 448, 417, 403, 406, 416, 407, 385, 160, 407, 432, 384, 405, 429, 416, 396, 86, 426, 438, 404, 399, 430, 402, 366, 53, 470, 427, 414, 410, 433, 383, 369, 75, 467, 427, 407, 396, 413, 418, 404, 147, 448, 431, 422, 364, 414, 437, 379, 81, 457, 399, 428, 413, 377, 429, 424, 128, 411, 428, 405, 385, 433, 427, 406, 97, 414, 442, 391, 406, 431, 399, 425, 157, 419, 404, 407, 392, 403, 430, 410, 107, 431, 400, 402, 412, 411, 433, 400, 141, 472, 400, 413, 406, 429, 421, 379, 82, 449, 402, 396, 426, 391, 429, 414, 153, 421, 427, 425, 426, 408, 390, 397, 95, 454, 432, 392, 387, 437, 451, 363, 151, 440, 406, 416, 435, 394, 439, 398, 95, 430, 431, 396, 371, 436, 422, 407, 178, 420, 440, 391, 406, 424, 415, 393, 98, 418, 424, 436, 403, 431, 426, 358, 22, 461, 433, 403, 409, 384, 429, 403, 99, 453, 417, 413, 390, 437, 434, 362, 171, 435, 441, 409, 407, 402, 437, 428, 94, 442, 398, 408, 442, 407, 419, 391, 145, 445, 444, 419, 389, 426, 402, 405, 74, 441, 455, 424, 393, 410, 423, 452, 146, 404, 434, 393, 403, 459, 429, 409, 93, 452, 395, 401, 445, 412, 456, 394, 119, 457, 451, 412, 403, 400, 450, 413, 91, 434, 441, 410, 385, 430, 432, 414, 149, 451, 412, 447, 403, 433, 404, 405, 95, 435, 415, 464, 396, 419, 427, 383, 165, 439, 448, 387, 418, 419, 457, 370, 91, 453, 461, 421, 385, 433, 435, 400, 171, 430, 454, 388, 426, 442, 408, 413, 95, 423, 441, 441, 388, 456, 421, 381, 68, 486, 411, 433, 421, 452, 418, 398, 98, 460, 430, 394, 428, 403, 473, 411, 163, 436, 408, 423, 392, 459, 446, 388, 95, 464, 436, 414, 411, 449, 437, 427, 141, 417, 443, 405, 410, 464, 454, 419, 98, 441, 438, 387, 432, 442, 416, 424, 153, 423, 466, 439, 385, 455, 446, 394, 81, 452, 461, 412, 420, 470, 402, 381, 147, 477, 475, 447, 396, 433, 451, 383, 99, 465, 425, 446, 431, 428, 437, 418, 171, 417, 465, 392, 431, 453, 439, 413, 98, 465, 444, 437, 424, 427, 432, 426, 151, 465, 445, 409, 405, 430, 482, 394, 104, 434, 460, 425, 418, 450, 440, 410, 168, 448, 435, 455, 402, 451, 454, 409, 96, 471, 443, 443, 405, 437, 469, 420, 3536, 520, 450, 443, 428, 462, 423, 431, 93, 468, 467, 426, 416, 434, 446, 432, 169, 438, 438, 448, 454, 418, 442, 455, 97, 452, 454, 446, 421, 425, 465, 425, 142, 484, 425, 474, 408, 418, 488, 405, 88, 475, 449, 439, 434, 473, 463, 435, 163, 437, 432, 469, 408, 473, 439, 449, 103, 449, 448, 463, 403, 431, 487, 434, 124, 500, 439, 434, 446, 475, 438, 432, 99, 488, 454, 423, 431, 438, 487, 450, 153, 470, 474, 396, 438, 467, 470, 411, 85, 471, 486, 439, 447, 437, 479, 413, 158, 477, 431, 472, 461, 438, 469, 421, 91, 472, 472, 437, 431, 471, 430, 458, 183, 498, 446, 445, 440, 472, 450, 411, 94, 470, 492, 448, 444, 497, 418, 392, 76, 548, 455, 459, 405, 462, 460, 450, 116, 497, 432, 462, 477, 454, 485, 402, 163, 512, 479, 437, 417, 482, 481, 428, 100, 493, 481, 452, 409, 464, 477, 457, 155, 486, 470, 451, 464, 483, 470, 399, 97, 501, 496, 455, 476, 432, 441, 480, 181, 466, 513, 441, 455, 449, 469, 434, 93, 494, 477, 483, 450, 461, 464, 470, 156, 508, 485, 446, 432, 496, 477, 425, 90, 506, 495, 435, 490, 454, 491, 467, 184, 494, 459, 456, 454, 462, 493, 457, 105, 502, 466, 465, 444, 506, 452, 502, 133, 503, 477, 474, 445, 471, 456, 463, 110, 538, 478, 480, 430, 481, 464, 465, 169, 519, 502, 474, 425, 488, 506, 436, 97, 506, 503, 485, 441, 492, 484, 415, 34, 563, 483, 460, 441, 485, 503, 458, 97, 515, 502, 455, 456, 490, 493, 466, 199, 520, 482, 447, 456, 500, 500, 467, 100, 544, 456, 484, 460, 480, 527, 456, 160, 533, 480, 480, 470, 483, 473, 453, 113, 545, 486, 490, 485, 500, 480, 472, 186, 515, 492, 493, 476, 495, 490, 477, 104, 535, 502, 483, 482, 478, 496, 483, 141, 554, 510, 480, 492, 489, 483, 493, 117, 515, 520, 507, 439, 495, 520, 501, 183, 519, 492, 481, 467, 523, 541, 456, 106, 548, 526, 445, 488, 503, 513, 490, 158, 527, 533, 506, 446, 489, 538, 480, 108, 567, 499, 467, 528, 482, 503, 519, 189, 508, 513, 494, 464, 514, 554, 469, 108, 544, 504, 500, 517, 492, 514, 440, 70, 607, 510, 512, 479, 547, 522, 462, 114, 553, 510, 514, 489, 510, 530, 530, 202, 510, 521, 482, 525, 524, 527, 486, 119, 553, 511, 488, 518, 506, 551, 527, 161, 535, 521, 509, 497, 543, 509, 475, 94, 595, 565, 518, 488, 539, 544, 479, 206, 529, 514, 540, 498, 573, 492, 500, 114, 567, 531, 517, 531, 555, 531, 466, 179, 580, 566, 489, 532, 534, 520, 510, 121, 545, 509, 573, 506, 544, 551, 528, 175, 555, 569, 506, 504, 572, 554, 484, 110, 568, 546, 521, 531, 555, 513, 533, 191, 574, 573, 505, 507, 525, 588, 522, 119, 611, 528, 512, 505, 545, 537, 536, 195, 587, 545, 564, 503, 565, 570, 515, 138, 549, 571, 504, 545, 585, 527, 505, 78, 716, 562, 512, 525, 605, 548, 501, 114, 617, 576, 508, 521, 600, 568, 552, 208, 540, 583, 547, 522, 554, 565, 543, 101, 589, 567, 551, 567, 566, 574, 573, 163, 604, 603, 537, 507, 573, 564, 566, 118, 592, 587, 550, 543, 570, 604, 502, 208, 626, 597, 564, 529, 556, 616, 538, 118, 598, 563, 547, 574, 593, 569, 565, 140, 699, 548, 546, 566, 588, 595, 550, 121, 644, 570, 534, 597, 583, 594, 560, 235, 593, 569, 585, 581, 600, 581, 560, 136, 614, 587, 566, 569, 593, 593, 562, 204, 622, 616, 588, 563, 600, 596, 548, 104, 671, 588, 594, 578, 625, 604, 580, 210, 611, 592, 593, 586, 596, 602, 545, 139, 664, 574, 624, 612, 566, 600, 579, 65, 719, 590, 607, 547, 641, 608, 576, 132, 651, 631, 602, 566, 612, 628, 597, 223, 674, 622, 581, 562, 598, 682, 587, 121, 646, 626, 613, 561, 673, 620, 598, 182, 698, 629, 603, 565, 631, 666, 588, 134, 676, 624, 645, 589, 611, 621, 632, 242, 631, 644, 610, 614, 669, 675, 587, 130, 643, 635, 612, 634, 610, 675, 605, 192, 723, 680, 619, 604, 676, 631, 584, 124, 685, 689, 647, 601, 671, 646, 621, 232, 668, 681, 625, 641, 649, 661, 589, 131, 719, 680, 628, 669, 647, 624, 638, 200, 741, 637, 654, 675, 641, 648, 659, 151, 708, 676, 645, 637, 645, 697, 654, 236, 695, 688, 639, 647, 689, 649, 656, 160, 739, 683, 675, 608, 660, 718, 535, 33, 813, 691, 682, 616, 702, 702, 674, 132, 718, 692, 681, 648, 684, 690, 642, 260, 746, 693, 727, 656, 689, 685, 668, 144, 756, 697, 636, 686, 705, 702, 665, 250, 769, 722, 671, 686, 723, 733, 656, 142, 714, 757, 678, 679, 699, 761, 663, 274, 786, 701, 695, 659, 760, 749, 653, 163, 707, 746, 758, 695, 673, 748, 690, 201, 834, 785, 732, 681, 724, 719, 710, 143, 778, 783, 716, 713, 729, 717, 749, 274, 796, 744, 748, 686, 763, 716, 736, 165, 785, 767, 731, 701, 725, 790, 705, 219, 829, 798, 715, 722, 784, 786, 720, 172, 802, 782, 706, 758, 800, 751, 766, 288, 775, 794, 783, 669, 755, 832, 758, 166, 833, 756, 762, 724, 837, 830, 692, 98, 894, 776, 764, 713, 848, 804, 736, 158, 856, 801, 758, 764, 818, 814, 772, 269, 871, 791, 790, 777, 807, 819, 803, 170, 840, 817, 777, 803, 820, 838, 832, 228, 853, 838, 753, 806, 819, 847, 812, 165, 886, 837, 824, 788, 805, 864, 818, 327, 885, 853, 769, 804, 891, 856, 769, 178, 903, 883, 813, 793, 909, 862, 775, 222, 1001, 864, 829, 817, 886, 900, 837, 163, 922, 860, 846, 825, 890, 899, 830, 333, 896, 943, 815, 821, 976, 878, 815, 159, 933, 920, 868, 889, 896, 894, 874, 321, 919, 962, 843, 882, 888, 944, 858, 188, 925, 953, 865, 875, 938, 913, 883, 329, 975, 929, 934, 870, 957, 989, 818, 191, 966, 937, 939, 895, 964, 965, 903, 8378, 1259, 952, 975, 940, 963, 960, 914, 201, 1051, 1013, 943, 928, 963, 1028, 985, 346, 977, 1052, 971, 925, 1003, 1028, 894, 201, 1106, 1023, 957, 978, 1002, 1053, 941, 308, 1112, 1017, 959, 1011, 1022, 1045, 982, 200, 1149, 990, 1079, 961, 1073, 1040, 1023, 388, 1084, 1050, 1063, 955, 1073, 1060, 987, 214, 1193, 1051, 1000, 1052, 1088, 1100, 1059, 260, 1287, 1133, 1028, 990, 1071, 1125, 1073, 223, 1177, 1134, 1103, 1046, 1127, 1072, 1096, 406, 1132, 1134, 1115, 1105, 1144, 1119, 1053, 198, 1249, 1167, 1134, 1040, 1163, 1153, 1153, 350, 1211, 1140, 1185, 1069, 1171, 1218, 1119, 214, 1252, 1242, 1151, 1116, 1157, 1206, 1165, 455, 1194, 1247, 1215, 1098, 1214, 1225, 1060, 244, 1366, 1237, 1223, 1187, 1233, 1322, 1051, 142, 1451, 1187, 1293, 1156, 1295, 1225, 1181, 254, 1378, 1311, 1228, 1138, 1318, 1272, 1254, 449, 1403, 1310, 1215, 1255, 1306, 1328, 1248, 229, 1431, 1398, 1302, 1227, 1285, 1351, 1343, 409, 1469, 1319, 1337, 1241, 1410, 1363, 1251, 273, 1528, 1413, 1343, 1291, 1387, 1393, 1341, 558, 1442, 1379, 1362, 1360, 1452, 1444, 1328, 317, 1511, 1428, 1401, 1360, 1433, 1483, 1461, 366, 1783, 1499, 1385, 1374, 1477, 1484, 1445, 276, 1611, 1502, 1505, 1432, 1532, 1504, 1540, 584, 1561, 1556, 1499, 1452, 1576, 1590, 1442, 324, 1661, 1623, 1578, 1510, 1584, 1635, 1564, 490, 1674, 1642, 1588, 1520, 1670, 1731, 1526, 346, 1800, 1662, 1621, 1606, 1702, 1644, 1604, 671, 1741, 1795, 1616, 1597, 1740, 1741, 1688, 335, 1886, 1783, 1656, 1687, 1746, 1873, 1363, 81, 2056, 1943, 1759, 1694, 1853, 1813, 1761, 354, 2008, 1817, 1806, 1786, 1945, 1907, 1828, 698, 1994, 1957, 1903, 1808, 1909, 2014, 1881, 393, 2074, 2002, 1873, 1861, 2070, 2033, 1952, 597, 2211, 2016, 2081, 1920, 2119, 2152, 1917, 393, 2319, 2140, 2023, 2059, 2129, 2179, 2096, 816, 2189, 2191, 2227, 2055, 2208, 2282, 2146, 440, 2427, 2373, 2184, 2108, 2271, 2387, 2227, 582, 2799, 2422, 2305, 2293, 2459, 2431, 2227, 448, 2645, 2579, 2392, 2302, 2603, 2465, 2439, 952, 2653, 2495, 2485, 2504, 2670, 2615, 2484, 523, 2853, 2759, 2547, 2529, 2715, 2811, 2686, 822, 2983, 2746, 2768, 2707, 2867, 2993, 2665, 568, 3154, 2980, 2844, 2850, 3032, 3052, 2984, 1149, 3150, 3124, 3065, 2896, 3318, 3284, 2844, 637, 3442, 3410, 3209, 3136, 3263, 3393, 3023, 429, 3955, 3420, 3499, 3231, 3554, 3672, 3325, 724, 3869, 3617, 3667, 3584, 3855, 3783, 3727, 1387, 4079, 3931, 3797, 3662, 4121, 4088, 3775, 831, 4453, 4244, 4046, 4013, 4383, 4125, 2208, 382, 486, 52, 3, 6, 2, 4, 4, 0, 8, 3, 3, 0, 2, 3, 5, 1, 3, 8, 0, 3, 3, 4, 3, 2, 2, 4, 1, 5, 4, 5, 0, 2, 6, 1, 3, 9, 2, 0, 3, 0, 3, 8, 3, 2, 2, 3, 2, 2, 6, 4, 2, 3, 2, 3, 6, 0, 6, 3, 4, 0, 6, 1, 3, 1, 5, 3, 3, 2, 1, 5, 6, 1, 2, 3, 2, 5, 2, 1, 5, 4, 2, 5, 3, 2, 2, 6, 1, 2, 4, 4, 3, 2, 1, 7, 1, 65 
Last edited by tony1tf on Fri Mar 26, 2021 3:27 am, edited 1 time in total.

User avatar
acedreamer
Posts: 1
Joined: Wed Feb 03, 2021 1:59 pm

Re: ADC Differential Non-Linearity

Wed Feb 03, 2021 2:11 pm

Nice piece of work!

Strongly suspect I'm not the first person to notice this, but there looks to be a correlation between multiples of 8 for the dips, and multiples of 256 for the really big dips. Probably worth trying to figure-out why this happens.
Dreaming the Future, which is now raspberry-flavoured!

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30436
Joined: Sat Jul 30, 2011 7:41 pm

Re: ADC Differential Non-Linearity

Wed Feb 03, 2021 2:35 pm

I've flagged this up to a HW engineering type guy to have a look.
Principal Software Engineer at Raspberry Pi Ltd.
Working in the Applications Team.

User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

Re: ADC Differential Non-Linearity

Wed Feb 03, 2021 2:59 pm

Thank you acedreamer and jamesh for creating some interest in this post. I will be interested in what the hardware guys say. This all comes out of my pre-retirement work in producing good quality spectroscopic data from ADCs.

Tony

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2669
Joined: Thu Jul 11, 2013 2:37 pm

Re: ADC Differential Non-Linearity

Thu Feb 04, 2021 10:46 am

What happens if you change your sweep limits to either side of the (val % 256 512 = 0) case?

I.e. toggle the GPIO when the ADC exceeds 400-600 or similar, and read out a few thousand samples.
Rockets are loud.
https://astro-pi.org

User avatar
scruss
Posts: 4580
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: ADC Differential Non-Linearity

Thu Feb 04, 2021 1:57 pm

The peaks are also at 512, 1536, 2560 and 3584, or odd multiples of 512 (1, 3, 5, 7).
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2669
Joined: Thu Jul 11, 2013 2:37 pm

Re: ADC Differential Non-Linearity

Thu Feb 04, 2021 2:16 pm

I'm not convinced the peaks at codes (2n+1)*512 for n=0,1,2,3 are real - we'd have spotted that during characterisation...
Rockets are loud.
https://astro-pi.org

dp11
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 700
Joined: Thu Dec 29, 2011 5:46 pm

Re: ADC Differential Non-Linearity

Thu Feb 04, 2021 2:24 pm

Some things to check. The ADC has and error bit , is that being set ? The PSU can be changed to a lower noise mode of operation via GPIO23 . can you try setting that ? Do also remember the ADC has an ENOB of 9bits

cleverca22
Posts: 5049
Joined: Sat Aug 18, 2012 2:33 pm

Re: ADC Differential Non-Linearity

Thu Feb 04, 2021 3:25 pm

dp11 wrote:
Thu Feb 04, 2021 2:24 pm
The PSU can be changed to a lower noise mode of operation via GPIO23 . can you try setting that ?
Screenshot_2021-01-30_08-09-47.png
Screenshot_2021-01-30_08-09-47.png (34.25 KiB) Viewed 2602 times

User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

Re: ADC Differential Non-Linearity

Sun Feb 07, 2021 4:29 pm

Thanks for your suggestions dp11 and jdb. Nothing makes any difference, so far. I have plotted narrow histograms around 511 with noisy and quiet power, and with only sweeping the sawtooth from 508 to 518. The plot here shows no discernible difference between the three methods:
Hist_compare.png
Hist_compare.png (141.43 KiB) Viewed 2502 times

dp11
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 700
Joined: Thu Dec 29, 2011 5:46 pm

Re: ADC Differential Non-Linearity

Sun Feb 07, 2021 4:50 pm

Next thing todo is to check the ADC error flag ( bit 9 of the ADC CS register )

User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

Re: ADC Differential Non-Linearity

Sun Feb 07, 2021 7:14 pm

I have discovered a problem - not understood yet. If I only sweep the output value between 506 and 519, there are no values below 504, but above the range there are histogram values all the way up to 4096 - lower numbers, but still there. Now I'll try looking at that error flag you mention, dp11 - thanks.

cleverca22
Posts: 5049
Joined: Sat Aug 18, 2012 2:33 pm

Re: ADC Differential Non-Linearity

Sun Feb 07, 2021 7:59 pm

just a random thought, this python code appears to be doing repeated one-shot conversions
when in one-shot mode, can the time to do a conversion vary?
might certain voltages need more/less time to convert, causing it to linger at a certain point on the curve and take more samples there?

Code: Select all

static inline uint16_t adc_read(void) {
    hw_set_bits(&adc_hw->cs, ADC_CS_START_ONCE_BITS);

    while (!(adc_hw->cs & ADC_CS_READY_BITS))
        tight_loop_contents();

    return adc_hw->result;
}
and i dont see any error checking in the code-path that micropython is using

could the answers be different, if the adc was set to continuous mode, and then you post-process the buffer in ram to generate the histogram?

User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

Re: ADC Differential Non-Linearity

Sun Feb 07, 2021 11:00 pm

dp11 wrote:
Sun Feb 07, 2021 4:50 pm
Next thing todo is to check the ADC error flag ( bit 9 of the ADC CS register )
Hi dp11 - I can't find any documentation on how to access this error bit - help please. However, I see some clues in that last post about possible ADC access too fast.
Tony

cleverca22
Posts: 5049
Joined: Sat Aug 18, 2012 2:33 pm

Re: ADC Differential Non-Linearity

Sun Feb 07, 2021 11:08 pm

tony1tf wrote:
Sun Feb 07, 2021 11:00 pm
dp11 wrote:
Sun Feb 07, 2021 4:50 pm
Next thing todo is to check the ADC error flag ( bit 9 of the ADC CS register )
Hi dp11 - I can't find any documentation on how to access this error bit - help please.
Tony
Screenshot_2021-02-07_19-04-25.png
Screenshot_2021-02-07_19-04-25.png (99.63 KiB) Viewed 2389 times
from the datasheet for the cpu

i dont see any simple api to access it from the sdk or micropython

User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

Re: ADC Differential Non-Linearity

Sun Feb 07, 2021 11:23 pm

Although I haven't found any documentation for the accessing the error bit, I tried slowing down the loop by adding a sleep(0.01) between ADC readings, and it didn't make any difference as seen in the graph here. Still using the narrow sawtooth to get results quickly in the narrow range.
Aha - see you have posted the relevant section of the processor data sheet - thank you - although it looks like I can't access the error bit from MicroPython. Here's a wider graph below, showing that even with the slow ADC loop, the reading are still strange.
Hist_compare2.png
Hist_compare2.png (164.88 KiB) Viewed 2376 times
Slow _hist.png
Slow _hist.png (77.09 KiB) Viewed 2354 times

User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

Re: ADC Differential Non-Linearity

Mon Feb 08, 2021 1:19 am

I finally got back to a reasonably smooth histogram by integer dividing the ADC reading by 128 to get it back to an 8-bit ADC. There are still two spikes in the histogram - at 63 and 191.
Hist_div128.png
Hist_div128.png (152.81 KiB) Viewed 2351 times

User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

Re: ADC Differential Non-Linearity

Tue Feb 09, 2021 9:24 am

I changed the code to use PWM to more slowly sweep the output around the desired range. Results the same as before in div16 mode to give 12 bit readings. If I print all ADC values and watch them scroll by, I can see the ADC get stuck at 511 for a while. Also getting those hits at higher values even when the signal is only sweeping to lower values.
Delay in loop makes no difference.
It all points to an ADC problem. I would be grateful if someone can give me some MicroPython code to change the access method so I can continue to explore this problem. Also code to access the error bit please.

User avatar
tony1tf
Posts: 100
Joined: Tue Jul 03, 2012 12:33 pm

Re: ADC Differential Non-Linearity

Tue Feb 09, 2021 11:15 am

Here's the latest full histogram with the PWM sweep going from 100 to 4000 at the ADC input after the LPF. Stopped when the 511 glitch got over 1000. It's still showing those big glitches and constant small ones
Hist_all_PWM.png
Hist_all_PWM.png (225.8 KiB) Viewed 2238 times

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30436
Joined: Sat Jul 30, 2011 7:41 pm

Re: ADC Differential Non-Linearity

Tue Feb 09, 2021 4:11 pm

If there is no C interface, just access the register directly, its dead easy - just check out some of the SDK wrappers for how it is done.

e.g. src/rp2_common/hardware_adc/include/hardware/adc.h

Might be worth reporting any missing functionality on the pico-sdk issue tracker.
Principal Software Engineer at Raspberry Pi Ltd.
Working in the Applications Team.

rgcoldeman
Posts: 18
Joined: Wed Feb 03, 2021 9:20 pm

Re: ADC Differential Non-Linearity

Tue Feb 09, 2021 4:29 pm

I haven't tried the ADC yet, but I confirm that with micropython it is quite doable to set up and use 'missing' peripherals with direct register access, either through the mem32[] function or the viper pointers.

As example I point here to an AWG implementation I posted yesterday on instructables. It sets up 2 DMA channels from micropython, all by using registers:

https://content.instructables.com/ORIG/ ... KV5OORM.py

Tony thanks for these studies, I follow this thread with great interest, just haven't got to play with the ADC yet!

James Adams
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 127
Joined: Wed Mar 19, 2014 2:58 pm
Location: Cambridge

Re: ADC Differential Non-Linearity

Tue Feb 09, 2021 5:10 pm

We are aware of the quirks with the ADC - the intent is to add something to the datasheet once we've fully understood the cause(s) and gathered some more qualification data (we're working with the IP vendor).
James Adams
Raspberry Pi - COO & Hardware Lead

bgolab
Posts: 377
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: ADC Differential Non-Linearity

Tue Feb 09, 2021 7:13 pm

We are aware of the quirks with the ADC
Has this issue been reproduced in your lab? Is this hardware related and not with the micropython ADC implementation?

bgolab
Posts: 377
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: ADC Differential Non-Linearity

Tue Feb 09, 2021 10:29 pm

I build a simple lab:
-DAC based on the PCF8591
-ADC based on the PICO

Probably the granularity is wrong as the PCF8591 is 8 bit only so I was not able to see the problem.
Attachments
adc_dacpng.png
adc_dacpng.png (28.31 KiB) Viewed 2109 times

bgolab
Posts: 377
Joined: Sat Jan 30, 2021 12:59 pm
Location: Krakow, PL

Re: ADC Differential Non-Linearity

Tue Feb 09, 2021 10:30 pm

The code:

Code: Select all

                adc_select_input(2);        // ADC2 GP28
                
                for(i=0; i<=255; i++){
                    pcf8591_buffer[1]=i;
                    i2c_write_blocking(I2C_PORT2, pcf8591_addr, pcf8591_buffer, 2, false);
                    printf("DAC/ADC, %d, ", pcf8591_buffer[1] );
                    sleep_ms(200);
                    adc_result=adc_read();
                    printf("%d\n\r", adc_result );
                }


Return to “General”