Summary of Simple 3 Button On-off With 12f629 (mikroC)
This article details a MikroC program for the PIC12F629 microcontroller to control three independent on-off switches using latching logic. The code initializes GPIO pins and uses a main loop to detect button presses, toggling LED states (GP0) based on input conditions from GP5. It demonstrates how to create a debounced latching relay effect without external hardware relays, suitable for controlling circuits or devices via software logic.
Parts used in the 3 Button On-Off Project:
- PIC12F629 Microcontroller
- MikroC Compiler
- Three Buttons
- LEDs (connected to GP0)
- Ground Connection
- Relay (optional for device control)
a simple 3 buttons on-off with pic12f629.

it’s written with MikroC
Step 1: The Code…

start the code with ”int”
———————————————————-
int x0,x1,y0,y1,z0,z1; ////// with this the GPIO outputs could stay on or off
void main() {
GPIO = 0x00; ////// all outputs are 0
CMCON = 0x07; ////// Disable CMCON PORT
TRISIO = 0b00111000; ////// inputs-outputs setup, 0=output 1=input (3 outputs/3 inputs)
x0=1;x1=0; ////// when the PIC starts x0=1,x1=0
y0=1;y1=0; ////// when the PIC starts y0=1,y1=0
z0=1;z1=0; ////// when the PIC starts z0=1,z1=0

while (1)
{
if ((GPIO.GP5==0)&&(x0==1)) ////// if the GPIO.GP5 grounded (pressed button) and the same time x0=1 (from the
begining of program, you see this at the previous step)
This will happen
{
x0=0; ////// the x0=1 it will be 0
x1=1; ////// the x1=0 it will be 1
GPIO.GP0=1; ////// turn the led on ( the – of the led is connected to the Ground and the + at GP0, pin 7 ) . delay_ms(600);
}
if ((GPIO.GP5==0)&&(x1==1)) ////// if the GPIO.GP5 grounded again and x1=1(from previous pressed button)
This will happen
{
x1=0; ////// x1=1 go back to 0
x0=1; ////// x0=0 go back to 1
GPIO.GP0=0; ////// now the led is off and the program wait for ((GPIO.GP5==0)&&(x0==1)) to turn on again .
delay_ms(600);
}
This is for the one button and led. You have two more. See the .txt file.
Step 3: Connection Diagram

you can use this at many projects as Latching Relay..
turn on and off circuits or devices with relay or not.
Source: Simple 3 Button On-off With 12f629 (mikroC)
- How does the code initialize the GPIO outputs?
The code sets all outputs to 0 by assigning GPIO = 0x00 at the start. - What is the purpose of CMCON = 0x07?
This command disables the CMCON PORT to configure the pins correctly. - How are inputs and outputs configured in TRISIO?
TRISIO is set to 0b00111000 where 0 represents output and 1 represents input for three outputs and three inputs. - What condition triggers the LED to turn on?
The LED turns on if GPIO.GP5 is grounded (button pressed) and the variable x0 equals 1. - Which pin connects the positive side of the LED?
The positive side of the LED connects to GP0, which is pin 7. - How does the program prevent rapid toggling?
A delay_ms(600) function is used after changing the state to stabilize the input. - Can this code be used for devices other than LEDs?
Yes, it can be used to turn on and off circuits or devices with a relay. - What happens when the button is pressed again while the LED is on?
The variables x0 and x1 swap values, turning the LED off and waiting for the next press to turn it on.
