import RPi.GPIO as GPIO

while True:
    val = 17
    val1 = val * 1200000/255
    print val1


A serial monitor (via Arduino) shows values in range 0-255 as sensor works, but raspberry don't take that values as val and solve for val1. all I see is,taking val as 17 and print 80000 constantly.


1 Answer 1


The pi GPIOs are digital, meaning at any given point they either have a value of 0 or 1. Since they can be read at a high frequency (the impediment here in general WRT the GPIOs is actually the nature of a multi-tasking OS, but for the ones with hardware timing where that issue is easily handled by a protocol interface such as UART, the frequency is at least 8 Mhz), this can translate into a stream of binary data.

But simply reading the value of a pin at any given point is still always going to return either 0 or 1. That doesn't explain your answer because as far as I can tell (I'm actually not a python user, but I am certain in this case) you aren't even doing that. Let's take a close look:

val = 17

Okay, we have an (integer) variable with a value of 17. Presumably this is the GPIO pin number you want to use, because that's how it is then applied. But next:

val1 = val * 1200000/255
print val1

This isn't reading the pin. This is equivalent to:

val1 = 17 * 12000000/255

And guess what that always is?

Again, I'm not a python user so I can't tell you the correct way to read a pin1 but that's your error.

1. I am also positive that is a question which does not need asking here or anywhere else as it must be clear from a glance at the documentation, and there must be thousands of examples besides online by now. If you want to write code, you do have to be able to read documentation and examples or it will take you forever to get nowhere.

  • yes, I do know that is the problem, but I want to know how to correct it. here val always 17 !! it should be variable value as it read GPIO. still I'm LOST!!
    – user52643
    Aug 30, 2016 at 15:11
  • No it shouldn't "be variable value as it read GPIO". The reason you're lost is because you have not bothered to read the documentation for RPi.GPIO or looked at any examples of how to use it. If you have, look again because I promise what they are doing is not what you are doing.
    – goldilocks
    Aug 30, 2016 at 15:17
  • I recognize that English isn't your first language and that this unfortunately is a bit of an obstacle with programming, because the programming languages are anglo-centric as is their primary documentation. I can't do anything about that and I believe it's simply an accepted fact in a lot of technical and engineering fields: The universal language is English. So you may have to slow down, focus, etc. If you want to do this you don't have a choice about learning that way. Good luck and remember to get up and walk around occasionally, it may help to clear the patterns in your mind.
    – goldilocks
    Aug 30, 2016 at 15:21
  • RPi.GPIO was good tip. others lol.
    – user52643
    Aug 30, 2016 at 15:45
  • ;) Please don't tell me that means English is your first language >_< ("Kids these days...")
    – goldilocks
    Aug 30, 2016 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.