I have a clock that has a circuit board with a resistor ladder setup for the buttons. I want to (disconnect the clock and) put a pi inside and use the buttons to control the pi. In normal use the board is supplied with voltage and the buttons connect to ground. The clock measures the voltage somehow (I dont think its from the ground side because If I connect the ground side of the board direct to ground it still works as expected.) If I connect the gpio pins to the buttons after the resistors but before the buttons the voltage gets backtracked through the resistor to the next resistor also. The result is that the pi detects more than one button being pushed at a time. This happens because the high side of the voltage ladder resistors are tied together. How can I wire this up to the raspberry pi without removing the resistors? schematic

  • \$\begingroup\$ are you quite certain that the schematic is accurate? \$\endgroup\$ – jsotola Jun 15 '18 at 3:27
  • \$\begingroup\$ electronics.stackexchange.com/questions/99417/… \$\endgroup\$ – User323693 Jun 15 '18 at 3:47
  • 2
    \$\begingroup\$ @jsotola why do you doubt the accuracy? Yes. I am sure it's correct. I think the answer of Jack Creasey is how it works not a resistor ladder like I thought. I still need to test it. \$\endgroup\$ – FoxSam12 Jun 15 '18 at 12:41
  • \$\begingroup\$ @Umar I am not sure what you mean... The question was not how to create a resistor ladder for Arduino. \$\endgroup\$ – FoxSam12 Jun 15 '18 at 12:44
  • \$\begingroup\$ Noo I was thinking about reading analog value to decode the button pressed. I still do t understand why we can't read individual analog values for individual buttons \$\endgroup\$ – User323693 Jun 15 '18 at 15:37

I'd suggest that the schematic you show uses a 'time to discharge' algorithm to decide which switch is pressed (or even if multiple switches are pressed).

On the R'Pi you could connect 'key' line to a GPIO pin using a 1k Ohm resistor (needed because of SW7) then do something like this:

  1. Set the GPIO to output a "1", this will charge up the capacitor (there might be switches pressed so the high voltage depends on which one(s) are pressed)
  2. Setup an interrupt on the GPIO going low
  3. Read the system timer (like this)
  4. Set the GPIO to input with no internal pullup
  5. When you get an interrupt read the system timer again and store it (the difference value is related to the discharge time of the RC network to the GPIO low level). Then you would execute a RET
  6. Back in your mainline code you have both the initial and second system timer value ….subtract the two to get the difference.

It's not very accurate, but good enough to find out which switch is pressed. It's going to be fun to catalog all the variations if multiple switches are pressed, but should be doable.

  • \$\begingroup\$ this looks interesting. I never thought of that option.. Is this what you are talking about?instructables.com/id/… \$\endgroup\$ – FoxSam12 Jun 15 '18 at 13:25
  • 1
    \$\begingroup\$ Yes, almost the same. In the link only one resistor is significant ...in your schematic the resistors are effectively in parallel when multiple buttons pressed. The software shown is a monolithic routine, for all the time you use it you can't do anything else. What I laid out was an interrupt based routine with little overhead. \$\endgroup\$ – Jack Creasey Jun 15 '18 at 15:48
  • \$\begingroup\$ good point about adding the series 1k ohm resistor. Timing results could depend on whether GPIO is "TTL", "CMOS", "Schmitt" type. \$\endgroup\$ – glen_geek Jun 15 '18 at 15:59
  • 1
    \$\begingroup\$ @glen_geek hardly likely to be TTL in a modern day MCU ...and here we are talking about a specific device (Raspberry Pi) and know it's not TTL or Schmitt I/O ports. \$\endgroup\$ – Jack Creasey Jun 15 '18 at 16:20
  • \$\begingroup\$ Can you elaborate a little please. I understand it like this so far: set gpio high set interupt to wait for going low ... wait for button ... when button is pressed gpio goes low read timer set gpio to input and floating and wait for interupt on high ... when its high read timer again and do the maths... is this correct? \$\endgroup\$ – FoxSam12 Jun 15 '18 at 16:38

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.