Looks like you have something else going on here.
If the switches are wired as shown and the resistance of a closed switch is less than 10K ohm, then your
read_button_volm() should work.
I'd start by narrowing down possibilities.
- Does the function return
1 if you manually short PB4 to ground with a jumper?
- Does the function return
1 if you manually short PB0 to ground and push the button?
- What voltage do you see on pin PB4 with a voltmeter when the button is pushed? Up?
Keep in mind that the internal pullup on the ATTINY12 can be as high as 122K ohm...
..and the voltage must be lower than 0.1 * Vcc on on of the digital IO pins that also has the XTAL function on them (PB4 & PB3)...
This means that to reliably detect a 0 on PB4 when it has the pull-up connected, you need the resistance between PB4 and ground to be less than...
122K / x < 0.1
x < 12K
So your switch must be less than 12Kohm to work reliably on this pin of this chip with the pull-up enabled.
If you absolutely must get this layout with these parts to Work, there are some tricks you can try.
The first one that comes to mind is using time and capacitance to detect the closed switch.
What you would do in your code is basically...
- Enable the pull-up on PB0
- Set PB4 to output, level LOW.
- Set PB4 to INPUT, no pullup.
- Time how long it takes for PB4 input to switch to HIGH with the button pressed.
You have basically made an RC circuit out of PB0, the switch, and the capacitance of pin PB4 and you are trying to measure the R of the switch by measuring the time constant of this RC.
Get a bunch of readings for this time count. This is how long the capacitance on the PB4 takes to drain to below the 0 bit threshold voltage.
Now you might be able to check if the button is pressed by repeating the above steps, but using the times you collected for a closed switch to set a timeout. If the pin goes high before the time, then the button is pressed, if it does not then it is not pressed.
Keep in mind that this approach has some limitations....
It is very sensitive to other paths that could discharge pin PB4 even when the switch is not pressed. For example, a moist finger or even some dirt across the traces could easily discharge the capacitance as quickly as your closed switch so you need to keep the board clean.
This takes much longer than just checking a bit value in a PIN register so is not time efficient.
You can't really set a pin interrupt on the button press because of the sequence involved. There are ways around this using the watchdog, but they are more complicated and less power efficient that a straight pin change interrupt.
Let us know if this approach works for you!