Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Analogue to digital
 
Share:
Notifications
Clear all

[Solved] Analogue to digital

10 Posts
2 Users
0 Reactions
2,348 Views
(@capriscott)
Active Member
Joined: 4 years ago
Posts: 5
Topic starter  

Hi, I would of thought this is quite a simple project but as I am new I would like some help on what to buy and then the programming to go with it. 

 

My problem is that to do this it costs me £150 using SPDT timer relays. So I have decided to start a new project using Arduino. 

 

I have a basic 3 gang light switch with 12v running through it and this will need to send a 12v pulse signal to a light driver (lumishore) to turn on. To turn the light off another pulse signal at 12v is needed. 

 

So each switch will provide a separate signal to their own driver. 

 

3 switches

3 drivers

3 lights

 

Any ideas what to buy?

What to download?

What code I will need?

A website I could follow?

 

Thank you 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Hi Capriscott,

I'm not familiar with Lumishore, but that may not be important 😉 

I have to admit that I'm not quite understanding what you're asking, so forgive me if I understood things wrong.
Do I get this right?

You have 3 lights (with each their own driver). To turn an individual light on, a 12V pulse needs to be send to the driver. Turning the light off again works the same way?
And the timer relays are used to keep the signal "up" long enough for the driver to switch (on or off, so keeping the 12V pulse long enough for the driver to pick it up).

In that case I'd get a simple Arduino Uno, or something like that.
Next I'd get one of these 4 relay blocks: Amazon.de, Amazon.com - you'll have one spare relay, but this way the relays are nicely kept together.
You can of course pick other relay options (Amazon.de, Amazon.com).
Obviously you'll need 3 simple push buttons.

I'd start with experimenting with one switch and one relay.
If you look at guides like this one (replace the PIR with the push button, as shown here), then the Arduino only has to respond to the button being pushed. When the button is pushed, the Arduino has to hold the relay for a certain time before it lets go again.

The schematics should look something like this (for one button and one relay - don't forget to connect +5V and GND to the Arduino):

The Arduino code could look something like this (I haven't tested it);

#define Button1Pin 2
#define Relay1Pin  8

int Button1State = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the Relay pin as an output:
  pinMode(Relay1Pin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(Button1Pin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  Button1State = digitalRead(Button1Pin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (Button1State == HIGH) {
    // turn relay on:
    digitalWrite(Relay1Pin, HIGH);
    // keep the relay on for a second (1000 = 1 second)
    delay(1000); 
    // turn relay off again
    digitalWrite(Relay1Pin, LOW);
  }
}

 

Once you have that working, you can assign each button their own pin on the Arduino, and the same for the relays.
The code should be easy to adjust as well, something like this (again: untested):

#define Button1Pin 2
#define Button2Pin 3
#define Button3Pin 4

#define Relay1Pin  8
#define Relay2Pin  9
#define Relay3Pin  10

int Button1State = 0; // variable for reading the pushbuttons status
int Button2State = 0;          
int Button3State = 0;

void setup() {
  // initialize the Relay pin as an output:
  pinMode(Relay1Pin, OUTPUT);
  pinMode(Relay2Pin, OUTPUT);
  pinMode(Relay3Pin, OUTPUT);
  
  // initialize the pushbutton pin as an input:
  pinMode(Button1Pin, INPUT);
  pinMode(Button2Pin, INPUT);
  pinMode(Button3Pin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  Button1State = digitalRead(Button1Pin);
  Button2State = digitalRead(Button2Pin);
  Button3State = digitalRead(Button3Pin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (Button1State == HIGH) {
    // turn relay on:
    digitalWrite(Relay1Pin, HIGH);
    // keep the relay on for a second (1000 = 1 second)
    delay(1000); 
    // turn relay off again
    digitalWrite(Relay1Pin, LOW);
  }

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (Button2State == HIGH) {
    // turn relay on:
    digitalWrite(Relay2Pin, HIGH);
    // keep the relay on for a second (1000 = 1 second)
    delay(1000); 
    // turn relay off again
    digitalWrite(Relay2Pin, LOW);
  }

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (Button3State == HIGH) {
    // turn relay on:
    digitalWrite(Relay3Pin, HIGH);
    // keep the relay on for a second (1000 = 1 second)
    delay(1000); 
    // turn relay off again
    digitalWrite(Relay3Pin, LOW);
  }
}

 

Alternatives to using an Arduino:

  • If you know somebody more technical than me, you could make your own timer relay.
    I believe, but could be wrong, a good capacitor may be able to hold the relay long enough for the signal to have the driver switch.
  • I would not be surprised to see the relays you mentioned on AliExpress for cheap.

 


   
ReplyQuote
(@capriscott)
Active Member
Joined: 4 years ago
Posts: 5
Topic starter  

Thank you for your reply. 

Sorry, I should not involved the timer relays they don't exist. It was just an alternative I could do without using arduino

 

The switches are latching also (not momentary).

 

Thanks


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

No problem.

If the switch should latch (eg. stay connected or stay disconnected) then you can accomplish that with this small change.
I'm saving the previous button state, to avoid that when a button was pressed for say a second or two, the code won't keep triggering the relay (which is probably harmless, but I'd rather be sure).

Note: Push buttons can have a so called "bounce" effect, where the Arduino sees a single press potentially as multiple button presses.
The resistor should prevent this from happening. However, if this still happens in your setup, then we can use a timer to catch this.

#define Button1Pin 2
#define Relay1Pin  8

int Button1State = 0;         // variable for reading the pushbutton status
int Button1PreviousState = 0; // remember last change, so we do not keep repeating

void setup() {
  // initialize the Relay pin as an output:
  pinMode(Relay1Pin, OUTPUT);
  // Start with relay off, this may not be required
  digitalWrite(Relay1Pin, LOW);
  
  // initialize the pushbutton pin as an input:
  pinMode(Button1Pin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  Button1State = digitalRead(Button1Pin);

  // check if the pushbutton is pressed and changed (to avoid repeats)
  if (Button1State <> Button1PreviousState) { // button state changed
    if (Button1State == HIGH) {
      digitalWrite(Relay1Pin, HIGH); // turn relay on
    }
    else {
      digitalWrite(Relay1Pin, LOW);  // turn relay off
    }
  }
  
  Button1PreviousState = Button1State;
}

   
ReplyQuote
(@capriscott)
Active Member
Joined: 4 years ago
Posts: 5
Topic starter  

This is brilliant. I am looking forward to getting more involved with this. Just waiting for Amazon to send everything through. 

 

One question is to the purpose of that resistor. Looks 5v will connect to the gnd when the push button is pressed. 

 

You're a great help


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

I do not fully know why the resistor trick works, as I'm unaware of some of the internals of the microcontroller.
What it in essence does, is cleanup the "noise" when pressing a button, which may been seen more than once as a button press.

(source)

There are quite a few solutions to debounce, either in hardware or in software.
In software you could do something like this: if a button is pressed again within a certain time, then ignore that it was pressed.
In hardware the resistor approach is the easiest, but there are more precise and more complex solutions.

Here a few more articles that may help explain:


   
ReplyQuote
(@capriscott)
Active Member
Joined: 4 years ago
Posts: 5
Topic starter  

Hi Hans, i received this error

 

In function 'void loop()':
Lumishore_Switches:22:21: error: expected primary-expression before '>' token
if (Button1State <> Button1PreviousState) { // button state changed
^
exit status 1
expected primary-expression before '>' token


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Yikes, that is what you get when you program in multiple languages haha.

The code

if (Button1State <> Button1PreviousState) { // button state changed

Should be:

if (Button1State != Button1PreviousState) { // button state changed

 

(change "<>" to "!=")


   
ReplyQuote
(@capriscott)
Active Member
Joined: 4 years ago
Posts: 5
Topic starter  

Hi Hans

 

Well i got there eventually as it all works. You were a great help and thanks for introducing me into the Arduino world and coding.

 

I had to change a few bits about  but it was fun understanding what was going on. Trial and error was my way forward.

 

thanks again for all your help, much appreciated.

 

Scott


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Hi Scott!

Good to hear you've got it working 😁 

And yes, trail and error is part of the fun exploring the Arduino world 😉 


   
ReplyQuote
Share: