Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT   

RELAY BYPASS: CONCEPTION


AND RELAY BYPASS CODE
STEP BY STEP THEORY TIPS

You might already have heard about "relay


bypass", or even used it without knowing it. It a
different true-bypass system than the classical
3PDT switch. Instead of using a mechanical 3PDT
switch, a soft switch, a microcontroller and a
relay are combined to turn the effect on and
off.


 So... Why bother? My 3PDT switch is great, don't
HOME
you think?STEP BY STEP
ALL POSTS SHOP BUILDS ABOUT

As you may already know, 3PDT switches are


the main cause of guitar pedal failure. These
switches are not particulary though, and they
often break, especially as we smash it
continuously with our feet on stage.

A classical high quality 3PDT switch is rated for


30,000 activation cycles. With relay bypass, we
use a relay that will play the mechanical role of
connecting ins and outs. Relays are usually rated
between 10 and 100 millions cycles! Thus, this
system is much more reliable.

Moreover, the soft switch that we use to activate


the guitar pedals also last longer than a 3PDT,
usually around 50,000 cycles! They are also
easier to replace, as there are only 2 connections
to make with the relay bypass system, and not
the full 3PDT wiring.

This blog post will present you how does it work,


and how to make your own relay bypass system
using a microcontroller, from the beginning to
the end! Long stuff (but good stuff?)!

Summary
How does relay bypass work?

Relay bypass circuit schematic
HOME
The microcontroller
ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
The relay
PIC/PC communication device: the
PICKIT2
PIC configuration bits
C source code to sense the switch

How does relay bypass work?

A relay works like a switch that is activated by a


given voltage. There is a coil inside; when it
receives a precise current, it will activate the
internal switch of the relay. Therefore, the
symbol looks like a switch:

The coil is symbolized with a rectangle, and the


internal switch by a classical switch symbol.
When the correct voltage is provided to the
relay, the switch is activated and change its
status.

The internal switch can be of different nature,


and some relays have an internal DPDT built in!
This will allow us to switch between "bypass" and
"effect on" status.

Unfortunately, relays with an internal 3PDT are
HOME
quite rare STEP
ALL POSTS
andBYreally
STEP
big...
SHOP
So how are ABOUT
BUILDS
we going
to do to light up the LED? We are going to use a
microcontroller. A microcontroller is a 8 pins
digital IC that is kind of a mini computer! We can
burn the microcontroller with a code that will tell
it precisely what it should do. Here, we are
going to say to the microcontroller : "activate
the relay when I press the switch, and light
up the LED"

Here is what it looks like in a commercial pedal


(Dr Scientist) :

The switch is connected to the microcontroller


that will "sense" when it is pressed: it will then
activate the relay and the LED: the effect is on!

It look simple, but trust me, it is not as easy as it


seems to implement ;)

Relay bypass circuit schematic 


HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
Here is the schematic of the relay bypass circuit:

The microcontroller is a PIC, the PIC12F675. PICs


are a very common type of microcontroller,
produced by the company MicroChip. There are
many different PICs, with a different number of
pins (from 8 to 64 or more!), several options
(analog / digital converters...etc). The PIC12F675
is an 8 bit PIC, quite cheap, small and with a lot
of options. It will be perfect for the relay bypass
circuit!


HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT

Of course, you can use a different type of


microcontroller, like the ATtiny85 that can work
with Arduino. I choosed the PIC12F675 because
there are many infos about it on the web, and
you can go a bit further than Arduino systems
with it. Also, it uses the C programming
language, which is a simple yet very functionnal
programming environement.

As you can see on the schematic, the PIC12F675


requires a 5V regulated voltage on its pin
number 1, and pin number 8 should be
connected to the ground. To supply the 5V
regulated voltage, we can use a voltage
regulator, the 78L05, which is commonly used
on circuit implementing a numeric chip, like the
Rub a Dub Reverb. The C1 capacitor will avoid
any variation of voltage due to the power supply.

Then, the relay is wired as a true bypass


DPDT: signal enter through one pin, and is
redirected either to the output (bypass), either to 
the input of the circuit board (SND).) The diode
HOME
connectedSTEP
ALL POSTS
to the relay SHOP
BY STEP
avoid the coil ofABOUT
BUILDS
the relay
to blow up the circuit when it is turned off (it can
generate a parasitic current).

The microcontroller

Each remaining pin of the microcontroller (pins


number 2, 3, 4, 5, 6 and 7) can be used either as
an input or an output. They are called GPIOs
(General Purpose Inputs / Outputs) and you
can configure them as you want with the code
we are going to burn in the microcontroller:
Input: the microcontroller can
"sense" what happens on this pin:
value of the voltage, pressed
switch...etc. It can receive "electrical
data" on this pin.
Output: the microcontroller can send
an electrical signal from this pin to the
components connected to it. If you
send a specific voltage or a voltage
with a precise frequency, you can
send "electrical data" to other
numeric chips like digital
potentiometers.
If we think a bit about it: the GPIO connected to
the LED will be an output (it will provide current
to the LED), same thing as well for the GPIOs 

connected to the relays. The GPIO connected to


HOME
the switchSTEP
ALL POSTS
willBYhave
STEP
to "sense"
SHOP
when the
BUILDS
switch is
ABOUT
pressed, therefore it will be an input. Thus, we
have 1 input (pin number 6) and 3 outputs (pins
number 2, 3 and 7).

BEWARE : in order to make it easier and


logic (irony inside...), the numbers of the
GPIOs are different from the number of
the pins... Here is a schematic:

Thus, pin number 2 is GPIO number 5, pin 6


is GPIO 1...Etc. In the code that we are going
to write, the GPIOs that we will use will have
to be named with the GPIO numbers, not
the pins numbers!

Good!

Each GPIO can also be configured to be analog


or digital:
Analog : an Analog to Digital
Converter (ADC) included in the chip
can be use to "read" precisely the
value of a voltage, on a 0 to 255 scale
(the PIC12F675 is an 8 bit chip, but 16
or 32 bits chips also exist, allowing

you to have bigger scales, like 0 to
HOME
1023).
ALL POSTS
We can alsoSHOP
STEP BY STEP
use aBUILDS
Digital to
ABOUT
Analog Converter (DAC) to send a
precise voltage on an output.
Digital : the chip will detect "on" or
"off" states, that is to say a "high" or
"low" voltage. It is like reading 1 or 0
for a computer. Here, the "high" value
will be 5V (it depends on the chip),
and "low" value 0V.
In our cas, we will use 5V to the relay and LED
when the effect is on, and 0V when it is off:
output will be digital.
To sense the switch, no need to use a "scale" for
sensing it: it is either on or off! So the input will
be digital as well.
Setting the states of the GPIO high or low will be
enough to fully control the relay and LED.

We will see later how to do that in the code


example.

The relay

There are many many different relays out there,


that will need different voltages to switch. They
have different sizes and shapes. Reed relay look
like integrated circuits, classical relays like big 
boxes, with different numbers of pins... Also,
HOME
there is specific
ALL POSTS
power SHOP
STEP BY STEP
neededBUILDS
to switch the
ABOUT
relay : the activation power. Here are a few
examples of relays:

So; we are going to use the high state of the


GPIOs to activate the relay. The "high state" is 5V
here, so we need a relay that can be activated by
a 5V voltage.

For our usage, we need a relay that is:


Small: if we want the circuit to fit
in a 1590A enclosure, we need to
make as small as possible. The
smaller the better!
Activation with a 5V voltage: 5V
is the "high" value that the
microcontroller can send from a
pin, so we need a relay that can be
activated with such a voltage.
Low activation power: the

microcontroller does not deliver a
HOME
highSTEP
ALL POSTS
intensity
BY STEP
output.
SHOP
Generally
BUILDS ABOUT
speaking, it is around 20 mA
maximum, which makes a 100mW
power on an output. Remember
that we can use a transistor to
amplify a bit the current if needed.
DPDT: we need a DPDT to switch
the input and the output at the
same time.
Affordable: we do not want this
system to cost 100 times the price
of a 3PDT... As it can last 3 times
longer than a 3PDT bypass system,
we can aim a total cost of around
3 times the price of a good 3PDT,
that is to say 10 euros
approximately. The soft switch
already cost around 3 euros,
which let 7 euros for all the
remaining parts, PCB included!
No surface mount devices: we
want something easy to assemble
and repair.

So I had to read a lot (like, a lot!) of datasheets of


different relays to find the perfect fit...

And the winner is...

The Takamisawa NA05W-K ! Small sized, cheap


(less than 2 euros!), 140mW activation power,
DPDT... Seems like a perfect candidate for what

we want to do!
HOME
The Panasonic
ALL POSTS
TQ2-L -SHOP
STEP BY STEP
5V seemed
BUILDS
also like a
ABOUT
good option: 140mW, a bit bigger though, and
more expensive (around 4 euros). However, it is
a latching relay which is a bit different from the
takamisawa that is a non-latching type.

Relay bypass code (in C)

So, we know what we are going to do, which


components we need... We "only" have the PIC to
code left to do!

PIC / PC interface
In order to code and burn the chips, we need
something to connect it to our computer, that
will allow us to burn the code in the
microcontroller.

For PICs, it is very simple: MicroChip


manufactures and sells an interface called
PicKit 2 (there is a version 3, but it is more
expensive and not very different from the
version 2). I strongly advise you to buy an
adapter that will make the overall process way
simpler than using a breadboard.

There are plenty of good deals online for the


PicKit, you can find it for 20 dollars easily. It is 
like buying a soldering iron: you can use it many
HOME
times so itSTEP
ALL POSTS
is worth
BY STEP
theSHOP
investment!
BUILDS
Also, do not
ABOUT
forget to buy a bunch of PICs (12F675).

As all the PicKit components (including source


code) is in open data (good call from MicroChip!),
there are some clones available out there. They
are usually less expensive, but buying "the real
thing" can save you from a lot of compatibility
troubles... So I strongly advise you to buy the
original PicKit2.

On your PC, you need three softwares to work


correctly with PICs:

1 MPLabX: it is a free software (well in its


simplified version that is perfect for us)
that you can download on the MicroChip
website. It allow us to code the PIC in C
(instead of assembly language that is a
low level, very complex language). Click
on the "download" tab on this webpage. 
HOME
XC8STEP
ALL2POSTS
compiler:
BY STEP
the
SHOP
compiler
BUILDS
willABOUT
transform the code that we write in C
into a understandable code for the PIC:
the hexadecimal format. You can
download it on the MicroChip website
("download" tab as well)
3 PicKit2 software: it allow your computer
to "speak" with the PicKit2 interface. We
will use this software in the final steps of
this tutorial to burn the code. Download
it here. (PicKit2 install A)

Setup everything.

Once you are done with the setup of the


softwares, place the PIC correctly on the adapter:

Connect it to the PIC correctly: the VPP / MCLR



pin should be connected to the pin of the PicKit2
HOME
with an arrow.
ALL POSTS
ConnectSHOP
STEP BY STEP
the PicKit2
BUILDS
to your
ABOUT
computer with the USB cable.

Now, open PicKit2 on your computer. It should


be able to detect the PicKit ("PicKit 2 connected"):

If it does not recognize your PicKit, disconnect


and reconnect the PicKit. It should work. All the
"3FFF" at the bottom of the "program memory"
window reprensent the memory of the PIC. We
will see the one burnt in our chip once we have
detected it.

Lets detect the PIC. In order to do that, you need


to select the type of Pic you are using. Click on
"Device Family", then "Midrange", and
"Standard":


HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT

If you click on "Read", it should detect the


PIC. Sometimes, it is quite difficult to get it
working. Verify that the PIC is correctly placed
on the adapter. Sometimes the ribbon cable can
be the problem: just connect the adapter directly
to the PicKit2. You can also disconnect /
reconnect the Pic or adapter. Once it detects it,
you should have something like this:

Good! We can program now!

PIC configuration bits


Before doing anything else, we need to configure
the PIC, in order to set the features that we need
or not. This step is absolutely necessary, the PIC
will not even initiate if you do not do it.

If you are lost at any step of this tutorial, you can


also watch this video that explain quite nicely
how to configure the PIC.

To configure the PIC, we first need to create a
project in MPLAB. To do that, open MPLab X,
HOME
click on "File",
ALL POSTS
"New Project",
STEP BY STEP SHOP
then
BUILDS
choose
ABOUT
"Standalone Project" and click "Next". You need
to choose the type of PIC that we are going to
use. Just type the  name of the PIC in the
"Device" bar, PIC12F675:

When ask to select a header, click on "none".


Then you have to choose the "tool" that is the
interface between the Pic and your computer.
Here, it will be the PicKit 2:

Then, you are asked to choose the compiler,


select XC8 and click on "Next". Then, name your 

project ("Relay Bypass for instance), and click on


HOME
"Finish". It STEP
ALL POSTS
will BY
create
STEP
a SHOP
folder in the "Projects"
BUILDS ABOUT
windows tab on the left, containing several other
folders:

Ok, now everything is set, and we can configure


the PIC properly.

Lets configure the PIC. To do that, we need to


create a header file that will contain all the
configuration information (="configuration
bits"). Click on "New", "File". A window will open.
Select the "C" folder (we want to program in C,
but you can program in other languages
depending on the folder you choose: C++,
assembler (good luck with that), XML...), and
select the "C Header File":

 Click on "Next", and choose the name of the



header file ("header" is good enough), then click
HOME
on "Finish".STEP
ALL POSTS
The "header.h"
BY STEP SHOP
fileBUILDS
shouldABOUT
appear in
the "header" folder:

Double click on this file and it should open in the


main window. On the bottom right corner, click
on the "configuration bits" tab:

In this menu, we have all the possible options to


configurate the PIC: oscillater, code
protection...etc. Here are the different options
and how we are going to configure them:
Oscillator Selection bits : the
oscillator is the clock needed for the
PIC. It can be either external (using a
quartz, and 2 GPIOs), or internal if the
PIC has an internal clock. The
PIC12F675 has an internal clock, so we
are going to use it, by using the
"INTRCIO" option. The clock is needed
here to use functions like __wait_ms()
that will allow us to pause the
program for a specific amount of time 
(in milliseconds).
HOME
Watchdog
ALL POSTS
timer SHOP
STEP BY STEP
: not necessary
BUILDS ABOUT
here: "OFF"
Power Up Timer enable bit : add a
72ms delay when the PIC is turned on.
Not needed here: select "OFF"
GP3 / MCLR pin function select : the
pin 4 of the PIC can be used as a
"reset button" (=MLCR), or as a classic
GPIO. Here, we do not need a "reset"
button, so select "OFF" to use it as a
classic GPIO.
Brown-out Detect Enable bit : the
"Brown Out" circuit is a simple
comparator circuit that checks that
the voltage supplied to the PIC is not
too low. It avoids memory corruption
issues if the PIC is undersupplied.
However, the PIC will draw more
current. As we do not use the internal
memory here, select the "OFF" option.
Code protection bit : do you want
your code to be visible by others or
no? If you want to protect your code
(for a commercial pedal for instance),
you can select "ON". Here, however,
as the code is public on my blog, no
need to protect it so you can select
"OFF" :)
Data code protection bit : PICs have
a small internal memory called
EEPROM. If you want, you can prevent
people from reading what is in it by
activating this option. Here, we do not
even use the EEPROM so you can 

select "OFF".
HOME
Once you are
ALL POSTS
done withSHOP
STEP BY STEP
that ("INTRCIO",
BUILDS
then
ABOUT
everything "OFF"), you can click on the  big
"Generate Source Code to Output" button. It
should display a code in the "Config Bits Source"
window. Select and copy this code:

You should have this in the main window:

Select everything from #ifndef, and delete


everything (yep!). Then paste the code we just
copy. You should have this now:

We are nearly done! One last thing: we have to


define the internal clock frequency. Here, we will
use a 4 MHz freuency. Just add this code under 
HOME
the configurations
ALL POSTS
bits SHOP
STEP BY STEP
you copied
BUILDS
before:
ABOUT

// Define oscillator frequency

#define _XTAL_FREQ 4000000

The "//" symbol allows you to write some


comments in your code, useful in order to
remember why you wrote that. I strongly advise
you to put as much comment as you need in
your code, in order to be perfectly sure that you
can understand everything when reading it. It
will be easier to reuse it later for other things this
way!
There it is! We are done with configuring the PIC.
Save everything, and we can code now.

Relay bypass code: sensing the switch and


activating the relay
First, let's create the file containing the main C
source code. To do that, click on "File", then
"New File". Click on the file C (to use C language),
then choose the "C Main File" type of file:


HOME
Then, choose
ALL POSTS
the nameSHOP
STEP BY STEP
of theBUILDS
file, andABOUT
click on
"Finish". The file should be placed in the "Source
Files" folders:

You should have something like this in the main


window:

Delete everything below the two #include, and


add these code lines : the two first ones add two
libraries that we need to make the code working:
"stdint.h" will allow us to use variables, and
"xc.h" will make our code compatible with the
xc8 compiler. Finally, we have to include the
configuration bits with the "header.h" file:

// Supplementary libraries
#include <stdint.h>

#include <xc.h> 
// Include configuration bits

HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT


#include "header.h"

You should have something like this now:

We can start now to code (finally!):


when the switch is pressed, we have
to know the state of the pedal, and
change it: if the pedal is off, we have
to turn it on and vice versa.
when the pedal is on, we have to
activate the relay and to light up the
LED.
We are going to code all of this in a loop that is
called the main() loop. The PIC will do again and
again every instructions embedded in that loop
starting from the top to the bottom. Write it like
this:

void main(void) {
}


First, we have to create a variable that will
HOME
define theSTEP
ALL POSTS
current
BY STEP
state
SHOP
of the pedal.
BUILDS
Here, I
ABOUT
named it "state". It can have two values : 1 (pedal
is "on") or 0 (pedal is "off'").
When the PIC start (when a power supply is
connected to the pedal), we can make the effect
off by defining the initial value of state equal to
0. Write this code inside the "{}":

uint8_t state; // variable


that will define the pedal's
state: on or off
state=0; // pedal is off when
starting

The first line of code create the "state" variable


that is an unsigned integer = positive integer ("u"
= unsigned, "int" = integer). Do not forget the
semicolon at the end of every instruction!

As you on the above examples, you can add


comments in your code with the "//" symbol. Do
it often in order to be sure that you understand
clearly every part of your code!

We have now to write some instructions to


correctly initialize the PIC, and to tell it what
things it should use or not. Here, as we use only
digital GPIOs, we do not need the DAC and ADC
converters. We are going to inactivate the
comparator as well (it is a circuit that can
compare the voltages between two pins of the

IC). To do that, we write this:
HOME ALL POSTS
ANSELSTEP
= BY0;
STEP SHOP
// no BUILDS
analog ABOUT
GPIO
CMCON = 0x07; // comparator
off 
ADCON0 = 0; // ADC and DAC
converters off

Lets define the nature of each GPIOs: we are


going to tell the PIC which ones are inputs and
which ones are outputs. Here, we need the
GPIOs 0, 4 and 5 to be outputs, and the GPIO 1
to be an input:

TRISIO0 = 0; // pin 7 is an
output (0 like "Output")
TRISIO4 = 0;
TRISIO5 = 0;
TRISIO1 = 1; // pin 6 is an
input (1 as "Input")
GPIO=0; // all the GPIOs are
in low state (0V) when
starting

Ok! The PIC is ready to start now! We are going


to place every other instructions in a while(1)
loop. The "while(1)" loop is a loop that will go on
forever as the PIC is on. The microcontroller will
execute every instruction from the top to the
bottom. When it arrives to the end of the loop, it
will start again from the top. Here is a small 
scheme to explain that:
HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT

The first intruction is executed, the


microcontroller goes to the second one (step 1).
After that, it switches to the third one (step 2). It
has arrived at the end of the loop, and goes back
to the first instruction! (step 3). It goes like this
forever as long as a current is supplied to the
PIC.

Here, there is no condition for the loop, as we


wrote while(1) so the loop goes on forever.
But we could have added a condition like
while(state == 1) for instance and the loop would
have only looped while the state of the pedal is
"on".

Here, the PIC is going to check continuously


the state of the switch. To do that, we add a "if"
statement. If the switch is pressed, the voltage of
the GPIO 1 will be 0V (the switch connects the
pin to ground). We have to write:

while(1) {
   if(GP1 == 0) { // if the
switch is pressed 
   } 
HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
}

In human language, that will be something like "if


the GPIO 1 voltage is low (= if the switch is
pressed), then execute every instruction
between the braces". Beware, do not forget the
two "equal" signs in the "if" statement (GP1 == 0)!

OK, so if the switch is pressed, we have to


change the state of the pedal. So if the pedal is
on, we turn it off, and vice versa. We can use two
"if" statements as well:

while(1) {
   if(GP1 == 0) { // if the
switch is pressed
      if(state == 1) { // if
the pedal is on...
         state=0; // ...turn
it off
      } 
      if(state == 0) { // if
the pedal is off
         state=1; // ...turn
it on
      }  
   }
}

HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
The variable state is changed when we press the
switch!

Debouncing
Well, practically speaking, it is not that easy...
When you press a switch, it does not usually
provide a clean edge. There are some "bounces"
and peaks, and the switch can be "on / off" very
quickly when pressed, during a very short
amount of time:

With the code above, the pedal would activate /


inactivate really quickly in an incorrect way when
we press the switch... In order to handle that, we
have to "debounce" the switch, that is to say...
Wait a bit!

We wait for the bounce period to be over (it is


really short), and check that the switch is still on.
To do that, we wait 15 milliseconds, then check
that the switch is still on. To do that, we will use
the __delay_ms() function, and then a if
statement to see whether the switch is still
pressed. This is why we had to configure the
oscillator in the configuration bits!

HOME ALL POSTS
void STEP BY STEP
main(void) SHOP
{ BUILDS ABOUT

   while(1) {
      if(GP1 == 0) { // if the
switch is pressed
         __delay_ms(15); //
debounce
         if(GP1 == 0) {
            if(state == 1) {
// if the pedal is on...
               state=0; //
...turn it off
            } 
            if(state == 0) {
// if the pedal is off...
               state=1; //
...turn it on
            }
         }
      } 
   }

__delay_ms(10);

There it is for debouncing!. We add 10 ms of


waiting at the end of the loop. You think that we
are done? Hell, no!

If you leave the code like this, if you keep your


foot on the switch, the state will change
really quickly from on to off! Pretty annoying... 

So, we have to add another "if" statement, to


HOME
change theSTEP
ALL POSTS
state of theSHOP
BY STEP
pedal BUILDS
when the switch is
ABOUT
released only! Before checking, we wait another
200ms (time to release the switch)

void main(void) {
   while(1) {
      if(GP1 == 0) { // if the
switch is pressed
         __delay_ms(15); //
debounce
         if(GP1 == 0) {
            __delay_ms(200); 
            if(GP1 == 1) { //
switch is not pressed anymore
               if(state == 1)
{ // if the pedal is on...
                  state=0; //
...turn it off
               } 
               if(state == 0)
{ // if the pedal is off...
                  state=1; //
...turn it on
               }
            }
         }
      } 
   }
}
__delay_ms(10); 
HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
You can try different __wait_ms() durations here,
I found that 200 ms was quite good: not too long
(we do not want to wait too much to activate the
pedal), and not too short (false positives)

OK, now that the pedal correctly changes its


state when we press the switch, we have to
specify to the microcontroller what it should
do when the pedal is on or off. This part is
easier. We will use a "if" statement: it checks the
state of the pedal, and adjust the states of the
outputs (activate or not the relay, turn the LED
on or not). Write this part before the end of the
while(1) loop:

if(state == 1) { // if the
pedal is on
   GP5=1; // activate the
relay
   GP4=0;
   GP0=1; // turn the LED on

else { // else (if the pedal
is off)
   GP5=0; // inactivate the
relay
   GP4=0;
   GP0=0; // turn the LED off

__delay_ms(10); // we wait for

the PIC to change the state of
HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
each GPIO 

We add 10 ms of waiting time in the end so the


PIC have time to change the state of the GPIOs: it
computes quicker than changing voltages on its
pins sometimes! Do not be afraid, 10 ms is really
really short, and there is no way you can "feel" it
while playing!

There it is! We are done !

Here is the full code:

#include <stdio.h>
#include <stdlib.h>
#include <stdint>
#include <xc.h>  
#include "header.h" // Include
the header (configuration
bits)

void main(void) { // main loop


   ANSEL = 0; // no analog
GPIO

   CMCON = 0x07; // comparator


off

   ADCON0 = 0; // ADC and DAC


converters off
   uint8_t state; // variable

that will define the pedal's
state: on or off
HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
   state=0; 
   TRISIO0 = 0; // GPIO 0 is
an output ("0" like "Output")
   TRISIO4 = 0;
   TRISIO5 = 0;
   TRISIO1 = 1; // GPIO 1 is
an input (1 like "Input") 
   GPIO=0; // Initially, all
GPIOs are in a low state
   while(1) {
      if(GP1 == 0) { // if the
switch is pressed
         __delay_ms(15); //
debounce
         if(GP1 == 0) {
            __delay_ms(200);
// switch released
            if(GP1 == 1) { 
               if(state == 1)
{ // if the effect is on
                  state=0; //
...turn it off
               } 
               if(state == 0)
{ // if the pedal is off
                  state=1; //
...turn if on
               }
            }

         }
      } 
HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
      __delay_ms(10);
      if(state == 1) { // if
the pedal is on
        GP5=1; // activate the
relay
        GP4=0; 
        GP0=1; // light up the
LED 
      }  
      else { // else (if the
pedal is off) 
         GP5=0; // inactivate
the relay 
         GP4=0; 
         GP0=0; // turn the
LED off
      }
      __delay_ms(10); // we
wait for the PIC to change the
state of each GPIO
   }
}

Already quite complicated for such a simple idea,


right?

Ok, so lets burn it in the PIC now! First, in MPLab


X, click on "Build Main Project" (hammer icon):


HOME ALL POSTS STEP BY STEP SHOP BUILDS ABOUT

If everything is OK, it should display "Build


successful" in the lower window:

If it does not work, it should tell you where is the


mistake / problem. Check everything with the
provided code above. Look for forgotten ";"
(most common mistake) or problems with "{}"
(second most common mistake ;)

Then, open the PicKit 2 program, and have the


PIC detected (cf beginning of this tutorial). You
can then load the "hex" program (= hexadecimal,
the only thing the PIC can understant) by clicking
on "File", then "Import Hex". The compiled file is
in the "MPLABXProjects" folder, then "Relay
Bypass.X" folder (name of your project), then
"dist", "default", and "production" folders:

It should have no problem importing it:


HOME
Change the
ALL POSTS
VDD for 5V,
STEP BY STEP
thenBUILDS
SHOP
click onABOUT
"Write"
and it should be programmed to the PIC!

Your PIC has your program loaded in it!


Check on breadboard that it works:
Going Digital - PIC coding

And you can assemble a PCB! (I am working on a


veroboard version of this)

On my side, I added a second switch to


choose between a classic activation, or
"hold". To do that, I used a second SPST switch
that will define is the mode is "hold" or
"permanent". I used GPIO 2 (pin number 5) as an
input to sense the state of this switch.

I added another variable called "temporary" that


will define if the activation is classic or in "hold"
mode. I let you think a bit about it, it is a bit more
complex than the above code, but you should be
able to figure it out! If you want, I will post the
source code one of these days :) 
HOME
There it is!STEP
ALL POSTS
Your first PICSHOP
BY STEP
project! As you
BUILDS
saw, it is
ABOUT
a lot of work to get it working. It is not very
complex (especially if you are already a bit
familiar with coding), but doing everything right
is quite long.

PICs can be used for many many different


applications. For instance, you can combine
them with digital potentiometers that can
replace classic potentiometers, and save your
presets or have a tap tempo for time-based
effects. Many possibilities! Once you have done
the relay bypass, you can imagine a lot of
different uses for PIC in guitar pedals!

Welcome to the digital world! :)

If you have any questions or comment, post a


comment and I will reply shortly :)
If this post was useful, thank me by liking the
Coda Effects Facebook page! Thank you!

HOME
To go further
ALL POSTS STEP BY STEP SHOP BUILDS ABOUT
Explanations about why you should use relay
bypass instead of 3PDT bypass (Ramble FX)
Datasheet of the PIC12F675 (132 pages pdf, good
luck reading that haha!)
Good tutorial in order to start with the
PIC12F675 (blinking a LED)
Video tutorial about GPIOs, very good for
beginners
Video introduction about PICs and how they
work
Good post about relay bypass on stompville
Another post about Relay Bypass by THCustom

RELATED POSTS
 What is guitar signal?
 Big Muff Ram's Head clone (73 version)
 Potentiometers and guitar effects
 Vemuram Jan Ray clone
 LPB1 boost
 Klon Centaur Clone and mods (Aion Refractor)

You might also like