Jump to content

@Aelita Sophie

 

The algorithm that the microcontroller should run is as follows:

 

// Initialization of the variables. The light should be off.

prev_light_state = False;
prev_wall_state = False;
prev_controller_state = False;

// The control loop. You could also use events, but that is more complex.
// This is just a general design to get you on the right path,
// I will leave the final implementation to you.

while True { 
  
  // Check if the wall switch has flipped states
  if get_current_wall_state() != prev_wall_state {
    prev_wall_state = get_current_wall_state();
    commanded_light_state = !prev_light_state; // The new light state should be the opposite of the old light state
  };
  
  // Check if the controller has flipped states
  else if get_current_controller_state() != prev_controller_state {
    prev_controller_state = get_current_controller_state();
    commanded_light_state = !prev_light_state; // The new light state should be the opposite of the old light state
  };
  
  // If the light state has been flipped
  if commanded_light_state != prev_light_state {
    pulse_latching_relay();
    prev_light_state = commanded_light_state; // Update the state of the light
  };
  
};

 

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

On 2/27/2017 at 2:47 AM, Aelita Sophie said:

What I need are 2 things:

- A toggle switch, controlled preferably 3.3 or 5v (As I'm planning to control it with either a Raspberry Pi or an Arduino), that "remembers" it's position even when power is not present. Just like a mechanical Toggle switch, but then controlled by a digital or analog signal.

- A way to detect if a certain line is "live". As in, currently is a closed circuit. For example, when I switch a light bulb on by a mechanical switch, I like to be able to report to either my Raspberry Pi or Arduino that the circuit is on.

 

 

 

You got an answer for the first one, the easiest would be a latching relay. You send a signal for a period of time (let's say at least 50 ms) and the relay switches to ON. You send the signal another 50ms and switches to OFF.  Some relays have two pins, one for ON and one for OFF.

 

As for the second one... the basics are ... you have digital pins on analogue pins on your microcontrollers.

Digital pins detect if there is some voltage on a pin, where "some voltage" means anything above some threshold, let's say 0.2v or 0.7v or some low voltage. Basically some low enough value but not low enough that it would be triggered by noise.

Analogue pins can detect a particular amount of voltage on the pin. Basically, you configure an internal voltage reference in the microcontroller, let's say 4.096v and then, the microcontroller converts any voltage between 0v and 4.096v  to a number between 0 and 1023, so 1 change for every 0.004v (10 bit means 1024 possible values).  You can write code inside the microcontroller to measure the voltage on the pin and if the value is above a minimum threshold, then you know the circuit is closed (because you receive voltage on the pin)

Now... you must be very careful not to exceed the voltage allowed on those input pins, which could be 5v or lower, let's say 3.3v if your microcontroller runs on 3.3v or is some low power microcontroller.

Also, you have to be very careful, not to connect directly any pin or part of your circuit to mains voltages, because it could be deadly.

 

To protect your circuit and yourself from dangerous voltages you'd have to use an optocoupler, an optical isolator.  It's basically a chip that inside has a LED on one side and a photo sensor on the other side and some air between them.  When the led lights up on one side, light from the LED hits the photo sensor on the other side and the photo sensor produces some voltage which can be detected by your microcontroller (arduino, raspberry pi)

 

Here's an instructable which shows how to use arduino to send a signal through an optocoupler and then on the other side, the photosensor produces a voltage which turns on a transistor that makes something work.  Basically, the microcontroller is completely separated from that circuit on the right, which runs at 12v (but could very well be some dangerous voltage). Here's the link : http://www.instructables.com/id/Isolating-circuits-from-your-arduino-with-optocoup/

 

Now to detect a mains or some ac voltage switch, you imagine it the other way around .. imagine the switch on the left side and the microcontroller on the right side, checking if the photo sensor produces some voltage.

 

The problem is that you have to convert that high AC voltage into a voltage that's just big enough to turn on that LED inside the optocoupler to produce some light.

A bridge rectifier (or a single diode in your case) can convert the AC voltage to DC. A small capacitor can hold some energy so that the minimum DC voltage would be above some threshold, enough for the LED to turn on ... and a resistor (about 75-100kohm would probably work for 110v AC) will make sure the current going through the led will be limited to some reasonable amount (for an optocoupler I would say 5mA is more than enough.

 

See this as well : http://electronics.stackexchange.com/questions/72672/using-an-opto-isolator-to-detect-120vac-on-a-microcontroller

 

Another version of this is to use a capacitive dropper to produce a low voltage, but this requires "special" film capacitor, which hobbyist may not have, and some other components. Basically it more or less creates an unregulated 5v power supply from mains, which then is used to power the led inside the optocoupler... it's a bit overkill, but guaranteed to work i guess : http://www.electroschematics.com/11922/mains-voltage-sensor/

 

 

 

Link to post
Share on other sites

9 hours ago, straight_stewie said:

-SNIP-

 

21 minutes ago, mariushm said:

-SNIP-

Took both your answers into count for this. As I like to keep things simple but functional. Both gave me great ideas in this case.

What I'm planning on doing right now is the following;

 

For switching I'm obviously using the latching relay. I'm going to use both outputs so I can have it trigger with the other 2 mechanical switches. (I believe it's called a Hotel Light Switch or something, where multiple switches can be in different states to turn on/off 1 light/output) Which would normally on a regular relay be called NO and NC. This way I can just simply trigger it without hassle.

 

Then at the end of the lightbulb, again keeping it simple. This is a combined idea I suppose. I'll just grab a 5v Power Supply, as tiny as I can find (or 12v and put a buck converter on it to 5v). Which will obviously be turned on when the light will be turned on. From that I can put an optocoupler if needed. As I only want a small current and voltage towards either the Pi or Arduino. (Will probaby use a Arduino of sorts, since low costs and more barebone thus smaller) This will trigger the "On" state in a script which I then send though probably Wifi to the "Mother" server, which is currently a Pi2 B+. From there I can use the webapp I made to actually read if the light is on. There I can also trigger the latching relay, which will send a post signal towards the arduino to trigger the actual latch.

 

Any thoughts on that?

Main RIG: i7 4770k ~ 4.8Ghz | Intel HD Onboard (enough for my LoL gaming) | Samsung 960 Pro 256GB NVMe | 32GB (4x 8GB) Kingston Savage 2133Mhz DDR3 | MSI Z97 Gaming 7 | ThermalTake FrioOCK | MS-Tech (puke) 700W | Windows 10 64Bit

Mining RIG: AMD A6-9500 | ASRock AB350 Pro | 4GB DDR4 | 500GB 2.5 Inch HDD | 2x MSI AERO GTX 1060 6GB (Core/Memory/TDP/Avg Temp +160/+800/120%/45c) | 1x Asus Strix GTX 970 (+195/+400/125%/55c) | 1x KFA2 GTX 960 (+220/+500/120%/70c) | Corsair GS800 800W | HP HSTNS-PD05 1000W | (Modded) Inter-Tech IPC 4U-4129-N Rackmount Case

Guest RIG: FX6300 | AMD HD7870 | Kingston HyperX 128GB SSD | 16GB (2x 8GB) G.Skill Ripjaws 1600Mhz DDR3 | Some ASRock 970 Mobo | Stock Heatsink | some left over PSU  | Windows 10 64Bit

VM Server: HP Proliant DL160 G6 | 2x Intel Xeon E5620 @ 2.4Ghz 4c/8t (8c/16t total) | 16GB (8x 2GB) HP 1066Mhz ECC DDR3 | 2x Western Digital Black 250GB HDD | VMWare ESXI

Storage Node: 2x Intel Xeon E5520 @ 2.27Ghz 4c/8t (8c/16t total) | Intel ServerBoard S5500HCV | 36GB (9x 4GB) 1333Mhz ECC DDR3 | 3x Seagate 2TB 7200RPM | 4x Western Digital Caviar Green 2TB

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×