# Need help controlling relay

**URL:** <https://forum.arduino.cc/t/need-help-controlling-relay/76829>\
**Category:** General Guidance\
**Created:** [November 3, 2011, 12:06am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829 "2011-11-03T00:06:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 12:06am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/1 "2011-11-03T00:06:23Z")

</div>

I used the standard relay plan using a diode, transister (TIP120) and uno.

The circuit worked fine with duemilanove, but when I tried it with uno it does not work right. Circuit stays on.

Can some one tell me what the difference between the two.

---

<div class="post-metadata">

**Author:** ![retrolefty](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@retrolefty](https://forum.arduino.cc/u/retrolefty)\
**Post date:** [November 3, 2011, 12:15am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/2 "2011-11-03T00:15:58Z")

</div>

There is no difference that would effect controlling a relay from an output pin. You must have some other problem with the wiring or the coding.

Lefty

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 12:21am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/3 "2011-11-03T00:21:48Z")

</div>

thank you.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 12:53am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/4 "2011-11-03T00:53:39Z")

</div>

Lefty if you are still here.

it is wire the same as duem, but the uno wont work.

here is the code:

const int ledPin = 7;  
const int Sensor = 1;  
const int threshold = 1;  
int sensorReading = 0;  
int pin7 = LOW;

void setup() {  
pinMode(7, OUTPUT);  
}

void loop() {

sensorReading = analogRead(Sensor);  
if (sensorReading \>= threshold) {  
pin7 = !pin7;  
digitalWrite(7, pin7);  
delay(1000);  
digitalWrite(7, LOW);  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 1:19am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/5 "2011-11-03T01:19:09Z")

</div>

The logic in your code looks a bit strange. As far as I can see, on the 'odd' sensor activations the output will flash high for a second, on the 'even' activations it won't do anything. Is that what you intended?

Is (!HIGH) equal to LOW and vice-versa? It might be, but to me that looks like a dodgy thing to do. If you're using pin7 as a state variable I'd prefer to see it given a more meaningful name and explicitly used as a boolean, then when you want to output HIGH or LOW you can use the ternary operator to convert your true/false into HIGH/LOW.

Where is your sensor input defined? Have you initialised the sensor? Have you confirmed that the sensor input is returning sensible values and your threshold comparison is doing what you intended?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 1:25am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/6 "2011-11-03T01:25:22Z")

</div>

I am a newbie,

I am trying to control a relay with a piezo sensor. When the sensor detects a vibration it triggers the relay. It worked with the duem board but not the uno.But now I know why it only detects everyother hit.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 1:30am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/7 "2011-11-03T01:30:53Z")

</div>

If you want it to trigger on every hit, get rid of all the logic associated with pin7 and just explicitly change the output to High and then Low.

I'd still start by checking whether your sensor input is giving you the values you expect and whether your threshold check is being passed when you generate your input. The rest of the logic isn't quite right but will do _something_. There isn't much initialisation there, and you may be relying on defaults which are not identical between the different Arduino implementations.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 1:37am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/8 "2011-11-03T01:37:51Z")

</div>

I can not even get to the sensor part. as soon as I plug in the power the relay starts going like it is the blink program. I just dont know why it works with the older board. I really need help here. I am trying to make an interactive target for soldiers. When the sensor determines that the target is hit the relay triggers a soleniod that drops the target.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 1:42am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/9 "2011-11-03T01:42:33Z")

</div>

This may be a dumb question, but are you sure it's actually running your code? Maybe you haven't downloaded it successfully and you are still running whatever was on the chip when you got it. If you're using the standard IDE you need to select the right type of Arduino so that the bootloader works.

---

<div class="post-metadata">

**Author:** ![Rokkit](https://avatars.discourse-cdn.com/v4/letter/r/b9e5f3/32.png) [@Rokkit](https://forum.arduino.cc/u/Rokkit)\
**Post date:** [November 3, 2011, 3:36am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/10 "2011-11-03T03:36:28Z")

</div>

Here's another shot in the dark. After the sketch is loaded into the board, take a 10uf capacitor and connect the +side to the reset pin and the - side to ground. Press the reset button and see if it starts working. You'll need to remove the capacitor if you need to reload the sketch.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2011, 3:52am UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/11 "2011-11-03T03:52:00Z")

</div>

Thanx everyone. I will try these things in the morning.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 4:35pm UTC](https://forum.arduino.cc/t/need-help-controlling-relay/76829/12 "2021-05-05T16:35:07Z")

</div>

Closed on May 5, 2021, 4:35pm UTC


