Connecting a button to PICAXE 20X2

Sean87

Member
Hi all,

How can I connect a micro button to 20X2 as an input? The button has 4 legs and I have a regulated 3V as supply to the chip and some LED's.

Thanks.
 

MartinM57

Moderator
Your button is just like a switch, so see the interfacing techniques in Manual 3.

You need to check which contacts close when the switch is pressed with a multimeter...
 

Sean87

Member
Your button is just like a switch, so see the interfacing techniques in Manual 3.

You need to check which contacts close when the switch is pressed with a multimeter...
OK I found the proper pins of the switch.

Now some questions.

1- What is the maximum voltage I can use for an input?
2- the following code does not compail (I am trying to check if c.6 is high then turn on some LED's)
Code:
if c.6 = high then 
high 0,1,2,3,4,5,6,7
endif
 

nick12ab

Senior Member
1- What is the maximum voltage I can use for an input?
2- the following code does not compail (I am trying to check if c.6 is high then turn on some LED's)
Code:
if c.6 = high then 
high 0,1,2,3,4,5,6,7
endif
Maximum input voltage is around Vdd + 0.3v so 3.6v at a 3.3v supply.

Replace 'if c.6 = high' with 'if pinC.6 = 1' as 'high' is a command and can't be used in an expression.

Also, since you're changing all pins on port B with the 'high' command, it may be better to use 'pinsB = 255' instead.

ADDED: Since you're using a pin which supports internal pullups, you can wire the switch like this and save on a resistor:



You will need to use this at the beginning:
Code:
pullup 2
and exchange any 'if pinC.6 = 1' with 'if pinC.6 = 0' and vice versa.
 
Last edited:

eclectic

Moderator
1. The supply voltage. (to be safe)

2.
Code:
if pinC.6 = 1 then ; ******

high 0,1,2,3,4,5,6,7

;or
; OutpinsB = %11111111  ; perhaps?
endif
 
Last edited:

Sean87

Member
1. The supply voltage. (to be safe)

2.
Code:
if pinC.6 = 1 then ; ******

high 0,1,2,3,4,5,6,7

;or
; OutpinsB = %11111111  ; perhaps?
endif
Thanks.

I am a bit confused. How can I do something that when I hold the button the LED be on and when I release it they turn off?
 

Jamster

Senior Member
In a loop keep testing the state of the buttons:
Code:
do
if pinc.6=1 then
high 0,1,2,3,4,5,6,7
else
low 0,1,2,3,4,5,6,7
endif
loop
 

Sean87

Member
ADDED: Since you're using a pin which supports internal pullups, you can wire the switch like this and save on a resistor:



You will need to use this at the beginning:
Code:
pullup 2
and exchange any 'if pinC.6 = 1' with 'if pinC.6 = 0' and vice versa.
Thanks, this pullup was super helpful!
 

Sean87

Member
In a loop keep testing the state of the buttons:
Code:
do
if pinc.6=1 then
high 0,1,2,3,4,5,6,7
else
low 0,1,2,3,4,5,6,7
endif
loop
Thanks this works also.

But question is that checking for button states always are linear for picaxe? I mean it just follows top to bottom of the program? What if the program is in another loop which does not have anything to do with inputs and in the middle user pushes a button which requires a urgent part of code to be ran. Is there somehing like EVENTS ?
 

westaust55

Moderator
Yes, the EVENTS you mention are called INTERRUPTS.

See the SETINT command in PICAXE manual 2(currently V7.7) page 215
 
Top