Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:06:43+00:00 2026-06-13T13:06:43+00:00

I’m playing with a Raspberry Pi using C#/Mono and Python. I’m currently converting some

  • 0

I’m playing with a Raspberry Pi using C#/Mono and Python. I’m currently converting some code from Python to C#, and the values are coming back different.

When adjusting a potentiometer and sampling these functions repeatedly I get 0-1023 in Python, 0-2047 in C#.

What accounts for the difference? I’m very very new to Python.

In python, this function produces a value between 0 and 1023 (when adjusting a potentiometer).

def readadc(adcnum, clockpin, mosipin, misopin, cspin):
    if ((adcnum > 7) or (adcnum < 0)):
            return -1
    GPIO.output(cspin, True)

    GPIO.output(clockpin, False)  # start clock low
    GPIO.output(cspin, False)     # bring CS low

    commandout = adcnum
    commandout |= 0x18  # start bit + single-ended bit
    commandout <<= 3    # we only need to send 5 bits here
    for i in range(5):
            if (commandout & 0x80):
                    GPIO.output(mosipin, True)
            else:
                    GPIO.output(mosipin, False)
            commandout <<= 1
            GPIO.output(clockpin, True)
            GPIO.output(clockpin, False)

    adcout = 0
    # read in one empty bit, one null bit and 10 ADC bits
    for i in range(12):
            GPIO.output(clockpin, True)
            GPIO.output(clockpin, False)
            adcout <<= 1
            if (GPIO.input(misopin)):
                    adcout |= 0x1

    GPIO.output(cspin, True)

    adcout >>= 1       # first bit is 'null' so drop it
    return adcout

In c#, it seems to return 0 – 2047.

static int returnadc(int adcnum, GPIO.GPIOPins clockpin, GPIO.GPIOPins mosipin,
 GPIO.GPIOPins misopin, GPIO.GPIOPins cspin)
    {
        int commandOut = 0;
        GPIOMem cpPin = new GPIOMem(clockpin, GPIO.DirectionEnum.OUT);
        GPIOMem moPin = new GPIOMem(mosipin, GPIO.DirectionEnum.OUT);
        GPIOMem miPin = new GPIOMem(misopin, GPIO.DirectionEnum.IN);
        GPIOMem cspPin = new GPIOMem(cspin, GPIO.DirectionEnum.OUT);

        cspPin.Write(true);
        cpPin.Write(false);
        cspPin.Write(false);

        commandOut = adcnum;

        commandOut |= 0x18;
        commandOut <<= 3;
        for (int x = 1; x <6 ; x++)
        {
            if ((commandOut & 0x80) > 0)
            {
                moPin.Write(true);
            }
            else
            {
                moPin.Write(false);
            }
            commandOut <<= 1;
            cpPin.Write(true);
            cpPin.Write(false);

        }

        int adcout = 0;
        for (int xx = 1; xx < 13; xx++)
        {
            cpPin.Write(true);
            cpPin.Write(false);
            adcout <<= 1;
            if (miPin.Read())
            {
                adcout |= 0x1;
            }
        }
        cspPin.Write(true);

        return adcout;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:06:44+00:00Added an answer on June 13, 2026 at 1:06 pm

    Right at the end of your Python implementation you are shifting out one bit of resolution:

    adcout >>= 1       # first bit is 'null' so drop it
    return adcout
    

    The same code is not present in your C# implementation:

    return adcout;
    

    Shifting right one bit is equivalent to dividing by two. So it makes sense that the C# version would return values twice as high.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.