Switch relay by connecting pin to ground


@ScruffR that line comment was for the conversion in Code1, thought was easier then duplicating the code on here, so where 2 or 3 appear in Code1 these are D7 and D0 when on the spark version :smile:

I cant get a good view from the camera but I tried to draw the traces I could see on that side


I don’t think this has been explicitly stated:

When you say ‘connected to GND to switch’, do you mean switch on - ie. its default state is off if unconnected - or switch off - ie. its default state is on if unconnected?


I could be wrong but, from the video it appears that:

IN1 = Floating, 3.3v High, 5v High ==> Relay: Common to NC, NO is open
IN1 = Low / Ground ==> Relay: Common to NO, NC is open

Is that correct @semaja2?


That sounds right @harrisonhjones except for “IN1 3.3V HIGH” and is what I was thinking. The 3.3V HIGH can be seen as the second example and the switched LED is always ON. Therefor the IN1 is always seen as a LOW to the RELAY because current flows through the Emitter-Base junction with 3.3V on IN1. Here’s the schematic I just whipped up based on the pics above.

EDIT: Now that we know what the resistor is there… we can calculate exactly how much current is flowing through the base when IN1 is 3.3V. (5V - 0.7V - 3.3V) / 1000 = 1mA which isn’t a lot, but potentially enough to drive that relay coil if the transistor has a high enough gain. That might even be a Darlington PNP which would be a high gain device.


Thanks for all the technical information, should send the eBay poster the wiring diagram and ask for some money :wink:

So it seems based on what we have found is that the Spark due to being 3.3V rather then 5V doesn’t have enough power to flick the relay, what would be the best way around this on the problem? Would it just to be switching between INPUT/OUTPUT modes or should I use extra circuitry?

I did some testing of the relay side and when the LED near VCC is on (or in the examples the red LED on the breadboard is off) the NO is connected, and NC is connected when the LED near VCC is off

The lower LED near GND on the relay is just to say its powered up :smile:

PS. Sorry for the delayed response we have had bush fires over here and my attention has been elsewhere :frowning:


@semaja2, you may want to look at this opto-isolated dual relay board on ebay that will work with the 3.3v of the Spark. Also, depending on the voltage you are switching, you could use a solid state relay (SSR). :smile:


Actually that’s not the reason… let me try to explain.

###This is what the relay wants (to be driven from a 5V system)

  • IN1 = 5V or OPEN, relay coil is not powered, NO to COM is open
  • IN1 = 0V, relay coil is powered, NO to COM is closed

###This is what happens when the relay is driven with a 3.3V system

  • IN1 = OPEN, relay coil is not powered, NO to COM is open (same as 5V system)
  • IN1 = 0V, relay coil is powered, NO to COM is closed (same as 5V system)
  • IN1 = 3.3V, relay coil is powered, NO to COM is closed (this is because the 3.3V is low enough of a voltage to turn the PNP (Q1) transistor on. The PNP will turn on in the above circuit at 0V, 1V, 2V, 3V, 3.3V and maybe 4V or so. This because current flows through R1and Q1’s Emitter to Base junction. This causes Q1 to turn on. The way to make current stop flowing is to explicitly set IN1 to 5V or OPEN.

Here are some links on how BJT transistors work:

Therefor a perfect way to drive your relay is:

// NOTE: DO NOT use D2 for the following IN1 control pin, it is NOT 5V tolerant.

// relay on
void relayOn() {
  pinMode(D6, OUTPUT);
  digitalWrite(D6, LOW); // set IN1 = 0V

// relay off
void relayOff() {
  pinMode(D6, INPUT); // set IN1 = OPEN

Hope all is well with those bush fires! That doesn’t sound as fun as hardware and software engineering :slight_smile:


Hey, this looks very similar to my question here, which was about driving a similar relay board with the same relay on it.

I used a 2N2222 transistor with a 2.2k resistor connected to its base pin. This is sufficient to drive either the relay on its own, or the input pin on this board, from 3.3V.

Put the other end of the resistor on a Spark Core digital pin, and connect the transistor’s emitter pin to ground.

You’ll have one pin left on the transistor, the collector. When Spark Core digital pin is on, this collector pin will pull to ground.

If you just care about the module, connect “IN” to the collector and you’re done. The module itself can then be connected to the Spark Core’s Vin pin (which will be the required 5V if you’re using USB power), with the third connection to Gnd.

If you want to do away with the module altogether and use raw components, the connections are one side of the coil to Vin (5V) and the other side to the transistor’s collector pin. You’ll also need a flyback diode in this arrangement (I had a 1N4004 handy) which goes across the two coil pins, with its bar end on the one that goes to Vin.

With either arrangement, you don’t need to change your code, the pin can be used as OUTPUT and when you write a 1, that means “on.”

Note, you do need the relay itself (or the module if you’re using that) connected to 5V as I described — the coils in this particular model do not fire reliably at 3.3V.

…Your optoisolator should be fine to sink the current on IN with the module (the onboard PNP transistors base current), but I had trouble finding one that could sink the coil current when moving to raw components.

Here’s the datasheet for that relay: https://www.ghielectronics.com/downloads/man/20084141716341001RelayX1.pdf


just scratching my head over this myself, and @BDub’s explaination makes perfect sense to me - but it just doesn’t work :frowning:

i’m on a photon, fwiw, but i gues that shouldn’t make a difference.

my relay is powerd via VIN and if i connect it’s “IN1” to ground or 3.3V the coil flicks just fine, but as soon as i try to use any of the digital pins no matter if set to high, low or input - nothing happens.

anyone got further ideas? :kissing:


testing a bit more i found this to be an issue with the rest of the code i implemented this in.

using D0 for the relay with the above functions while D6 was set to LOW and D5 was set to HIGH just didn’t work.

using a clean sketch doing nothing else it did work.

so i swapped pins to make D2/D3 the high/low (powering a DS18b20) and am using D6 now for the relay it seems to work fine. though i don’t have the sensor hooked up here on the test-board.