I am a novice when it comes to arduino code but managed to get my UNO R3 to work with a 4 channel 12V relay for a previous theatre prop piece but it is months since then and I am having trouble with this one. I want to have a linear actuator pull a pulley mechanism to extend some small prop pieces, then some lights to come on and then the actuator to retract the prop piece and turn the lights off. I have wired up the UNO r# to the relay and am using three of the relays #4 to retract the actuator #3 to extend the actuator and #2 for the LED lights. So I wired positive 12 v to the normal open port and 12v negative to the normally closed port and then the centre port goes to my actuator. I did the same for #3 For #2 I ran 12v negative direct and then ran the centre wire to the LED strip
switchState = 0;
void setup(){
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
switchState = digitalRead (2);
// wait for button to be pressed
if (switchState == LOW) {
// the button is not pressed
digitalWrite(8, HIGH); //actuator OFF
digitalWrite(9, HIGH); //actuator OFF
digitalWrite(10, HIGH); // LED OFF
}
else { // the button is pressed
delay(8300); // wait for 8.3 SECONDS
digitalWrite(10, LOW); //actuator EXTEND
digitalWrite(9, HIGH); //actuator OFF
digitalWrite(8, HIGH); //LED OFF
delay(131000); // wait for 2 MIN 11 SEC
digitalWrite(10, HIGH); //
digitalWrite(9, HIGH); //
digitalWrite(8, LOW); //LED ON
delay(57000); // wait for 57 seconds
digitalWrite(10, HIGH); // actuator off
digitalWrite(9, LOW); //actuator on
digitalWrite(8, HIGH); //LED OFF
delay(5000); // wait for 5 SECONDS
}
} // go back to the beginning of the loop
` nt switchState = 0;
void setup(){
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
switchState = digitalRead (2);
// wait for button to be pressed
if (switchState == LOW) {
// the button is not pressed
digitalWrite(8, HIGH); //ATENATOR OFF
digitalWrite(9, HIGH); //ATENUATOR OFF
digitalWrite(10, HIGH); // LED OFF
}
else { // the button is pressed
delay(8300); // wait for 8.3 SECONDS
digitalWrite(10, LOW); //ATTENUATOT EXTEND
digitalWrite(9, HIGH); //
digitalWrite(8, HIGH); //LED OFF
delay(131000); // wait for 2 MIN 11 SEC
digitalWrite(10, HIGH); //
digitalWrite(9, HIGH); //
digitalWrite(8, LOW); //LED ON
delay(57000); // wait for aquarter second
digitalWrite(10, HIGH); //
digitalWrite(9, LOW); //
digitalWrite(8, HIGH); //LED OFF
delay(5000); // wait for 5 SECONDS
}
} // go back to the beginning of the looptype or paste code here`
Can anyone spot something wrong in my code? I can hear the relays click open but no acuator action. I have checked the 12v and it powers the actuator and the LED. I am running 12v + from my source into the VCC and negative into ground. I also have a momentary switch wired in to input 2 using the 5v Arduino board
Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
I’m not 100% with your description of the relays - it looks like if the don’t respond identically then you will have a short circuit .
You would be better off with a relay with two sets of contacts to avoid that .
The relays I’m guessing you are using are the little blue things - these are no good for switching inductive loads , or significant DC loads .
You maybe able to do your task just with simple time delay relays , which would be easier to setup
If you hear that relays click, probably problem is not in your code.
Take a multimeter and measure voltage between relay "outputs" when "clicking" . It should show 12V, 0V, -12V...
Are you sure your PSU is able to supply enough current for actuator and leds??
Post specs of them.
Can you please post your code in code tags, as described in the above link?
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Hmm...Schematic and part number list please? When I have used a relays with a linear actuator, I used 2/4 on an opto isolated (so there's no electrical connection between the Arduino and relays) relay board like this
I like to use ATX PC Power supplies converted to project power supplies (it's much easier than you think, I promise), here's a guide to do exactly that:
In the application I'm thinking of that I made closest to what you're doing, I used a linear actuator to open and close a Hallowe'en treasure chest based on the input of 1/9 infrared "magic" wands. Since you have just a pushbutton it seems, it will be simpler.
Two things:
If that's a pushbutton, do you have a pulldown resistor? If not, you pin is floating (the Arduino doesn't know what HIGH or LOW look like in your project.
Also, you have no button debouncing. You should have that.
If you don't feel like adding a pulldown resistor, no problem, Arduino has your back.
Simply change this
to this
pinMode(2, INPUT_PULLUP); // also, for Pete's sake, name your pin assignments, man!
and invert your program logic (so if (switchState == LOW) { will be
if (switchState == HIGH) {
and vice versa.
About that code I had, give it a look (and note how I posted it in the forum correctly, using code tags - that's the culture here) and see if it helps you figure stuff out. Oh, note that in this sketch rxChar is literally a char read in from the central controller Arduino to activate various functions.
/*
+5V from arduino to VCC beside the input pins on relay module (for optoisolators)
pin 7 (orange), pin 6 (yellow). GND to chassis ground
relay module +12v from psu through buck converter to relay output 3 NC (ground) and relay output 4 NO (+12v)
jumpers from relay 3 NC to relay 4 NC and from relay 3 NO to relay 4 NO.
Relay 3 common to red motor wire, relay 4 common to black motor wire.
Jumper for JD-VCC/VCC removed (to enable optoisolation and so arduino doesn't have to power the module
electromagnetic coils. JD-VCC powered by PSU +5V rail
NB, these relays are active LOW, so LOW turns them on.
NO for devices that you want to switch on that are normally off, NC for devices that are usually on that
you might want to interrupt.
*/
char rxChar;
boolean newData = false;
const int forwards = 7; // pin for relay board
const int backwards = 6; // pin for relay board
const int readyLed = LED_BUILTIN;
void setup() {
pinMode(forwards, OUTPUT);//set relay as an output
pinMode(backwards, OUTPUT);//set relay as an output
pinMode(readyLed, OUTPUT);
digitalWrite(readyLed, LOW);
Serial.begin(115200);
}
void loop() {
recvOneChar();
showNewData();
switch (rxChar) {
************ full open-hold(some duration)-close functions ******
case ('b'):
normalLong();
break;
case ('o'):
normal();
break;
case ('p'):
halfShort();
break;
case ('u'):
halfMilli();
break;
case ('v'):
quarterShort();
break;
case ('w'):
quarterMilli();
break;
case ('x'):
eightShort();
break;
case ('y'):
eightTiny();
break;
case ('z'):
eightTease();
break;
/************ end full functions ***********************************/
/************ core functions/discrete operations ********************/
case ('s'):
stopAct();
break;
case ('h'):
holdAct();
break;
case ('f'):
fullExtend();
case ('e'):
extend();
break;
case ('a'):
halfE();
break;
case ('t'):
quarterE();
break;
case ('c'):
fullRetract();
break;
case ('r'):
retract();
break;
case ('l'):
halfR();
case ('d'):
quarterR();
break;
case ('k'):
holdLong();
break;
}
/******************** end discrete operations ************************/
/******************** end switch/case *********************************/
digitalWrite(readyLed, LOW);
}
/******************** end void loop(){} *******************************/
void recvOneChar() {
if (Serial.available() > 0) {
rxChar = Serial.read();
newData = true;
}
}
void showNewData() {
if (newData == true) {
newData = false;
}
}
/************************** COMBINED FUNCTIONS *********************/
void normalLong() { // 1 minute
extend();
holdLong();
retract();
}
void normal() { // hold 30 sec
extend();
holdAct();
retract();
}
void halfShort() { // hold 3 sec
halfE();
stopAct();
halfR();
}
void halfMilli() { // hold 1.6
halfE();
milliStop();
halfR();
}
void quarterShort() { // hold 3
quarterE();
stopAct();
quarterR();
}
void quarterMilli() { // hold 1.6
quarterE();
milliStop();
quarterR();
}
void eightShort() { // hold 3
eightOpen();
stopAct();
eightClose();
}
void eightTiny() { // hold 1.6
eightOpen();
milliStop();
eightClose();
}
void eightTease() { // hold 0.8
eightOpen();
microStop();
eightClose();
}
/************************* DISCRETE FUNCTIONS *******************************/
Activate the relay one direction, they must be different to move the motor
this particular actuator takes 20 seconds each way
/************************* OPENING FUNCTIONS *********************************/
void fullExtend() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(22000);
}
void extend() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(20000);
}
void halfE() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(10000);
}
void quarterE() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(5000);
}
void eightOpen() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(2500);
}
/************************** CLOSING FUNCTIONS ************************************/
void fullRetract() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(22000);
}
void retract() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(20000);
}
void halfR() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(10500);
}
void quarterR() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(5500);
}
void eightClose() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(2500);
}
/******************************** STOP/HOLD FUNCTIONS *******************************/
void stopAct() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(3000);
}
void milliStop() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(1600);
}
void microStop() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(800);
}
void holdAct() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(30000);
}
void holdLong() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(60000);
}
Final note: Serial char handling method courtesy of the legendary @Robin2, one of the real gurus here in the Arduino forum. The following page is worth learning from if Serial comms in Arduino is a bit of a mystery to you:
Thank you to those who have replied and made suggestions. The Arduino is an Arduino UNO R3, The relay unit is a 4 channel keyestudio unit with 4 x SRD-12VDC-SL-C relays:
I discovered that the small jumper pin that connects the GRND and the COM was missing from the relay board and once I replaced that I gained control and the program runs. I took the suggestion to remove the momentary switch and ran the program just using delays so it runs more simply. The code now is simply:
pinMode(7, OUTPUT);//Output to Relay 4
pinMode(8, OUTPUT);//Output to Relay 3
pinMode(9, OUTPUT);//Output to LED strip
}
void loop() {
digitalWrite(7, LOW);//Initialise pin 7 as LOW
digitalWrite(8, LOW);//Initialise pin 8 as LOW
digitalWrite(9, LOW);//Initialise pin 9 as LOW
delay(8000);//delay 8.3 seconds
digitalWrite(7, LOW);//Initialise pin 7 as LOW
digitalWrite(8, LOW);//Initialise pin 8 as LOW
digitalWrite(9, HIGH);//Initialise pin 9 as HIGH this opens the actuator
delay(131000);//delay
digitalWrite(7, LOW);//Initialise pin 7 as LOW
digitalWrite(8, HIGH);//Initialise pin 8 as HIGH this turns the LED lights on
digitalWrite(9, LOW);//Initialise pin 9 as LOW
delay(189000);//delay for 3min 9 seconds
digitalWrite(7, HIGH);//Initialise pin 7 as HIGH this closes the actuator
digitalWrite(8, LOW);//Initialise pin 8 as LOW
digitalWrite(9, LOW);//Initialise pin 9 as LOW
delay(5000);//delay 5 seconds
digitalWrite(7, LOW);//Initialise pin 7 as LOW everything should be turned off
digitalWrite(8, LOW);//Initialise pin 8 as LOW
digitalWrite(9, LOW);//Initialise pin 9 as LOW
delay(120000);//delay 2 min - this gives them time to turn the off button OFF
}
And you do you do, always, but I would lose every comment like these
digitalWrite(7, LOW); // Initialise pin 7 as LOW
and change the others too
digitalWrite(8, HIGH); // this turns the LED lights on
It is pointless noise to repeat in a comment what even a fairly inexperienced reader would already know if she were reading the code. Such gratuitous comments are a distraction, and a nuisance to maintain. They can become misleading or wrong if editing fails to also keep all the comments up to date.