Need help with ARDUINO nano tap tempo triggering a relay circuit

Started by dlogreen, October 13, 2020, 08:56:04 PM

Previous topic - Next topic

dlogreen

Hey guys,

I was lucky enough to get in touch with Deadastronaut after seeing his Arduino nano tap tempo post. He admitted that others in here would be way more helpful in helping me with this idea, so I thought I'd make a post to introduce myself and beg for assistance :)

I'm pretty money with analog stuff and can solder just about anything, but I've literally never fooled with the digital stuff aside from swapping a few diodes to color my OD pedals to my taste.

On this post https://www.diystompboxes.com/smfforum/index.php?topic=121506.msg1142740#msg1142740 he was successful in getting an singular LED to blink using a tapped in tempo...which is exactly what I want to do, except I want that output to the LED to instead go to a 12V relay to power something like an LED strip or just larger, more powerful light of some sort.

I also found this video on YT, https://www.youtube.com/watch?v=AK-u71Lk5VI that did a pretty solid job of how to breadboard a relay to activate the 12V led strip.

What I do not know is how to take the program from the tap tempo circuit (which would also be paralleled to my delay pedal momentary button) and then signal the relay circuit code shown in the youtube video.

I can sort out power and things like that, but I just have no experience with Arduinos, but this could be a great intro, and turn out to be something we can use on stage in place of an actual click track, since I tap in tempo of every song the band plays (lead vox n guitar).

I appreciate the awesomeness of everyone here already... .I'm poked around and found suggestions for mods and tweaks before, but never thought about asking any questions since I didn't have anything original (I hope this is original).

Thanks in advance, and I really hope someone is willing to help. The guys would flip when they saw it at the next show.

dlogreen

Here's the tap temp code.... so then how to send the output signal to a relay board/circuit? Also, the text between the  :icon_redface: emojis was on the post I mentioned earlier, but is that okay to leave in the print or was that only there as an FYI? I just don't want to think I've got it ready to roll and then it fail due to some text that I didn't know shouldn't be there. :)



/*
    Tap Tempo
   
    An experiment to detect tempo by tapping a button
   
    Press a button connected to digital pin 12.
    LED on pins 2-11 displays the beat pulse
    LED on pin 13 shows the current button state (dim when button is pressed)
   
    Digital Output 2-11 pins <----- 220 ohm ----+[LED]----> GND
   
     :icon_redface: (Digital 13 is wired up to the LED on the uno/nano board.  If you have an older
     board, drop the standard LED between pin 13 and GND) :icon_redface:
     
    Digital 12 <---------[ switch ]--------> GND

*/

void setup()
{
  pinMode( 12, INPUT_PULLUP );   /*   /* tap button - press it to set the tempo */

  pinMode( 13, OUTPUT );  /* button state display - not necessary */

  for( int i=2 ; i<12 ; i++ ) {
    pinMode( i, OUTPUT );  /* tempo display light - shows the current tempo */
  }
}


int lastTapState = LOW;  /* the last tap button state */
unsigned long currentTimer[2] = { 500, 500 };  /* array of most recent tap counts */
unsigned long timeoutTime = 0;  /* this is when the timer will trigger next */

unsigned long indicatorTimeout; /* for our fancy "blink" tempo indicator */

void loop()
{
  /* read the button on pin 12, and only pay attention to the
     HIGH-LOW transition so that we only register when the
     button is first pressed down */
  int tapState = digitalRead( 12 );
  if( tapState == LOW && tapState != lastTapState )
  {
    tap(); /* we got a HIGH-LOW transition, call our tap() function */
  }
  lastTapState = tapState; /* keep track of the state */

  /* check for timer timeout */
  if( millis() >= timeoutTime )
  {
    /* timeout happened.  clock tick! */
    indicatorTimeout = millis() + 30;  /* this sets the time when LED 13 goes off */
    /* and reschedule the timer to keep the pace */
    rescheduleTimer();
  }

  /* display the button state on LED 2 */
  digitalWrite( 13, tapState );

  /* display the tap blink on LED 13 */
  for( int i=2 ; i<12 ; i++ ) {
    if( millis() < indicatorTimeout ) {
      digitalWrite( i, HIGH );
    } else {
      digitalWrite( i, LOW );
    }
  }
}

unsigned long lastTap = 0; /* when the last tap happened */
void tap()
{
  /* we keep two of these around to average together later */
  currentTimer[1] = currentTimer[0];
  currentTimer[0] = millis() - lastTap;
  lastTap = millis();
  timeoutTime = 0; /* force the trigger to happen immediately - sync and blink! */
}

void rescheduleTimer()
{
    /* set the timer to go off again when the time reaches the
       timeout.  The timeout is all of the "currentTimer" values averaged
       together, then added onto the current time.  When that time has been
       reached, the next tick will happen...
    */
    timeoutTime = millis() + ((currentTimer[0] + currentTimer[1])/2);
}

PRR

Welcome.

There is a standard way for any particular CPU to drive a relay. Similar to driving an LED, but more current, so semi-specific to the CPU you use. Hang on Arduino websites and forums to find it.
  • SUPPORTER

dlogreen

thanks, I'm still digging for more.

I don't have have high aspirations for arduino, but I found this relay code, which is pretty much what I want it to do... but how to incorporate it into that tap code.

I get that the tap is the input, so that would probably stay the same, but the output is where I'm not sure how to link it up.

/*
* This is the Arduino code for Dual Channel 5V Relay
* to control turn ON or OFF AC or DC load
* Watch the video https://youtu.be/58XWVDnB7Ss
*  *
* Written by Ahmad Nejrabi for Robojax Video
* Date: Dec 26, 2017, in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purpose only.
*
*/

void setup() {
  pinMode(7, OUTPUT);// connected to S terminal of Relay

}

void loop() {

  digitalWrite(7,HIGH);// turn relay ON
  delay(3000);// keep it ON for 3 seconds

  digitalWrite(7, LOW);// turn relay OFF
delay(5000);// keep it OFF for 5 seconds

garcho

Maybe I'm misunderstanding you but wouldn't you just write to pin 7 for the relay control, in addition to or instead of pin 13 for the LED? Is your relay expecting a 5V control signal? How are you going to protect the arduino, transistor? Look up stuff about connecting the arduino to relays before firing anything up. Turning a relay on and off might not be the best way to implement what you want to do, can you be more specific?
  • SUPPORTER
"...and weird on top!"

ElectricDruid

Quote from: dlogreen on October 13, 2020, 09:53:59 PM
thanks, I'm still digging for more.

I don't have have high aspirations for arduino, but I found this relay code, which is pretty much what I want it to do... but how to incorporate it into that tap code.

I get that the tap is the input, so that would probably stay the same, but the output is where I'm not sure how to link it up.

I don't think you need the "relay" code at all. All it does is turn an output on for a bit. Which is exactly what the tap tempo code already does - in fact, it flashes a whole collection of outputs at the tap rate.

All you need is a little interface circuit (a relay driver circuit) to get you from an Arduino output to a relay, and then you can power whatever high power stuff you want through the relay and it'll flash at the tapped rate. The code is done, I'd say.

HTH,
Tom

dlogreen

Thank you Garco and Electricdruid,

After spending the next couple hours watching videos on arduino stuff and relays specifically, you both are right.  I wasn't aware how the signal wires worked on the arduino versus say a motorcycle electrical system. But essentially, I want the LEDs to function like the turn signal on a motorcycle....so a relay is exactly what I want. 5V send to the relay which would then open and close a circuit running 9-13V to power some leftover LED lights I have that should be bright enough to see on any stage.

Now that I realize naming the terminals really isn't all that important, I can just wire the relay signal wire in place of the LED leg. The relay is OPTO isolated for protection (thanks for pointing that out Garcho...). Like I mentioned before, I'm fine with electronics and circuits in an analog sense, and have built entire wire looms for bikes, and wired all of my guitars and such, but I'd never fooled with electroprocessors like this, so I wanted to make sure I didn't bite off more than I could chew on a pipe dream.

Parts were ordered last night once I thought I had everything I needed. I ended up with a mechanical relay instead of SS because a) there weren't any prefabbed boards available immediately, and I didn't want to have to deal with low/high trigger stuff. That could be something in the future if these mechanical relays turn out to be noisier than I planned.

Ultimately, the tap tempo leads will be shared with the actual signal to my delay pedal (I'll use a diode to prevent any feedback from the arduino circuit if needed). The relay circuit will be housed in a powered project box with a few different outputs, so I can use jacks and plugs to connect multiple LEDs, or small vibrating motor units, or anything that can serve the purpose of being a reference for keeping time.

I'll definitely keep you guys updated in case anyone else is curious to see how it all pans out.

Thanks for the help!!!

anotherjim

There are cheap "maker modules" for the job consisting of one or more relays. The current drive from the controller I/O pin is provided and the relay contacts are very well isolated on the far side of the PCB. Almost bombproof.
One such here...
https://steps2make.com/2017/10/arduino-5v-relay-module-ky-019/
There are many brands of these out there and some also use an opto-isolater in the control input for extra isolation from the relay, but that isn't really necessary when the load is low voltage DC.
... and if you want to go mad heres an 8 channel with optoisolated inputs (the 4 pin chips are the isolators)...
https://coeleveld.com/arduino-relay/

These usually have a little relay activity LED on the boards for extra confidence when testing your contraption.
To be honest, you don't really need relays to operate LED's, just suitable driver transistors, but if relay operation is easier to get your head around, it won't cost much more money or time.




garcho

QuoteTo be honest, you don't really need relays to operate LED's, just suitable driver transistors

yeah, that's what i was thinking, in analog audio world, it's easy to forget transistors were invented as electronic switches. don't need optos or anything like that either. same idea, GPIO sends control voltage to base, turns switch on/current moves through power transistor.
  • SUPPORTER
"...and weird on top!"

FiveseveN

I was going to say... you need a transistor to drive the relay anyway, why not skip the relay?
Quote from: R.G. on July 31, 2018, 10:34:30 PMDoes the circuit sound better when oriented to magnetic north under a pyramid?

PRR

Relays clack. And wear-out in many thousands of clacks. I have a relay in my Honda which gives trouble after a while.

Yes, national phone systems WERE relay operated and worked well. But the transistor was essentially invented (found) to reduce the use of relays in the phone system.

Go ahead and do a relay to get the idea demonstrated to you and your bandmates. But I think longterm some solid-state technique will be better. (So why is my Honda FULL of relays, even things with actual switches?)
  • SUPPORTER

dlogreen

Quote from: FiveseveN on October 14, 2020, 02:55:15 PM
I was going to say... you need a transistor to drive the relay anyway, why not skip the relay?

Thank you all for the input, but the reason I thought relay first is because I'm familiar with putting them to use in another setting, and I'm not used to Arduino ANYTHING, so worrying about saturation values, and how to assign a certain output, or calculating resistor values just seems like more work than I'm willing to do right now as a proof of concept model. I'm aware that relays can definitely go bad, but in this case, they are readily available in a mechanical version , and the one on my motorcycle is 30 years old and still works, so in this controlled setting, I think it'll be fine unless it really turns to crap.

Remember, I'm taking a basic LED circuit that I just barely understand and trying to use it loosely for what it was intended. I'm planning to also use a DPDT footswitch to disconnect the signal wire and LED main power simultaneously, so we're not left with clicking and flashing going on when we aren't on stage.

The idea has grown from its infant stages, but I still don't want to over-complicate the circuit now that I think I have a grasp. I'd need a bit more time to understand how to control the specific components if I went a different direction. I think space not being a huge concern is a part of why micro components aren't immediately the goto for me.

If anyone could recommend a suitable transistor to use as a switch, that'd be cool... but I feel like I'd still end up using a SSR in the end if the mechanical fails, and that way I could still choose whatever voltages I want on the other end without worrying too much of frying the circuit.

All that said, I really do appreciate the help, but it's definitely hard taking one unknown and adding additional unknowns to it :-/

dlogreen

Got my boards and components in this weekend, and tonight I successfully setup the mechanical relay and the SSR with a 12v ultrabright LED normally used for a turn signal.

Onstage the mechanical would get lost in the mix, but standalone it's definitely loud enough to be noticed. The SSR is money though, and it's super fast and consistent.

I have a feeling the guys are really gonna like this concept after they see it in person :)

Thanks for all the guidance guys!

PRR



My age is showing: turn signals have been yellow for decades, eh?
  • SUPPORTER