I have a raspberry pi connected to a mains relay, which is connected to an ac lamp and outlet onto a breadboard. I want to add a photoresistor module to the raspberry pi/breadboard but I'm afraid it will affect the relay somehow? The raspberry Pi is going to be used as a switch for the lamp, I'm pretty new to working with mains so I'm being very cautious.

  • Add a photo of your existing wiring and describe what your going to connect the photoresistor to. – CoderMike Nov 29 '18 at 16:45
  • 2
    You're right to be cautious, breadboards are not supposed to be used with voltages above 30-40V. Actually, the best suggestion I have is to only experiment with mains when your device is inside a closed box. If it blows up, all you'll have to deal with will be a minor fire. If you do it on a breadboard, you may get that blown up stuff in your face, or touch something inadvertently and get zapped. – Dmitry Grigoryev Nov 30 '18 at 11:04

Yes, please be careful working with mains voltages as they can be lethal ! Ideally, all of your mains wiring to the relay should be in a separate enclosure that's properly grounded. If you're unsure about how to do this, please do your homework or ask questions here.

As far as the photoresistor module controlling the relay state; the key is understanding what your module does. In other words: it's unlikely that you will be able to control the relay directly with a photoresistor as it's simply an analog device whose resistance changes as a function of the incident light intensity. Typically, photoresistors are used to bias a transistor (i.e. turn it on or off depending on the light intensity), and the transistor then controls the relay by switching current through the relay's coil. In other words: If your photoresistor module has the components needed to control current through the relay coil, then you're in business. If not, you'll need to add those to your circuit. Edit your question to add a link to the modules specifications, and we'll try to be more specific.

There are numerous ways to use a photoresistor to control a relay. (Here are several examples).

If you design and build your own "module", one issue in particular to be aware of is an apparent oscillation of the switching device (transistor) as the input nears the threshold value at which it is designed to switch. This frequently occurs in photoresistor circuits, but is effectively controlled by adding hysteresis. I'll stop here as there's little point in discussing how to add hysteresis if your module already has that designed in. But if you decide to build your own module, you can edit your question, and we'll try to help you with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.