Relay and bash problem

Viewed 43 times

I have a pi 3B and a relay board – “5V relay module is active LOW, meaning that LOW level will activate the relay on and HIGH level will deactivate it.” .

I have connected 5V to Vcc on the relay, ground to gnd and another pin and Pin 12 to ch1.

This setup works as the relay will turn on with movement but the relay goes straight off.

I am using motioneye to “run a command” and “run an end command” in the movement notification section.

I need the space on the SD card so didn’t want to install Python although I can if required. It also seems a bit OTT for a simple GPIO output.

My two bash scripts are below and they have bee given 755 permissions.

lightOn.sh ( run a command )


echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction
echo "0" > /sys/class/gpio/gpio18/value
# I have tried adding sleep 60s here

lightOff.sh ( run an end command )


echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction
# I have tried adding sleep 60s here
echo "1" > /sys/class/gpio/gpio18/value

I have no experience with bash and have no idea why the above will not work; I have added sleep 60s without success in different places.

Can somebody point out what I am doing wrong?

Note: If I swap the values around the relay never goes off.


I may be getting a bit further and although the site I brought the relay from says it needs to be pulled low it works the other way around BUT only once! It works every time running the script from Putty; so I assume there is a different problem somewhere.

This is my current code but as I say it will only run once and it does not do exactly what I want. I may be better off with a latching relay as I can send a on and off signal from different pins. I will discuss it with a guy at work next week who will be able to help me out.


echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction
for i in {0..500..1}
 echo "1" > /sys/class/gpio/gpio18/value
sleep 0.1
echo "0" > /sys/class/gpio/gpio18/value

@tlfong01 I didn’t think you down voted me as the post was down voted a while before you posted. It annoys me when people down vote and do not leave a reason why; how am I supposed to improve the post if I do not know the problem?

I have seen ” Let me google and see if I can help: ” used before by people who are insinuating the OP has not put any effort into finding an answer. I am sorry to have assumed that was what you were saying. I have probably spent about 16hrs now trying to sort this out. Sometimes not having the correct search phrase can be a problem to finding the answer.

 New contributor
  • Ah, I don’t know nothing of MotionEye. Let me google and see if I can help: “How does MotionEye work?” raspberrypi.org/forums/viewtopic.php?t=238708#p1458704 – tlfong01 yesterday
  • Thank you for your anonymous down votes and passive aggressive reply. I do have Motioneye working and have spent quite few hours trying to get the next step working. Believe me my last resort was asking a question here as I was half expecting this sort of thing. – Bonzo 16 hours ago
  • Hi @Bonzo, I am sorry to have misled you thinking that I have downvoted your question. To clarifyl, I did NOT downvote your question. Actually I have not downvoted any question or answer for the last couple of months. I think my down vote or upvote ratio in this forum is perhaps 1 to 95. Just now I upvoted your question, to convenience you that I did not downvoted you. Comments welcome. – tlfong01 14 hours ago
  • Now coming to the “passive aggressive reply”. I am not sure if my words “I don’t know nothing …” is impolite or rude. I am using these words because I know they are used by those who do not speak proper English. Actually I was an ex-refugee and only started learning alphabets A, B, C when I was eight years old. One thing I think I might have misled you may be I said I might google and see if I can help later. Unfortunately I did not carry on and because I got tied up in other answers I already started and need to spend more time to complete. / to continue, – tlfong01 14 hours ago
  • Actually my first comment is only stating a fact that I have not heard of MonionEye, and that I would google to learn more. Let me see if I can remember what I thought after reading the thread of posts I referred. My first impression is that it is a new thing worth looking deeper. I did watch the youtube video referred in the first or second post and found it interesting. Actually I noted the web link but did not recommend it in time, therefore causing the misunderstanding. – tlfong01 14 hours ago
  • One other reason that I did not answer right away is that your question is actually in two parts, the first part is on low trigger relay, second part on MotionEye. Actually low trigger relay is my favourite subject. As usual, didn’t know nothing about Arduino/Rpi relay when I joined this and another forum last year. If you search relay KY019 in this and other forums, you might find that my posted started with my fist thread is over 1,000. – tlfong01 14 hours ago
  • Ah, now I remember what I think about the MotionEye video. (1) The video is by a popular guy who made many other videos, and I rank him top 5% of all the educational video I watched. – tlfong01 14 hours ago
  • (2) I have been playing with object detection for a long while, using AI, NN, PIR etc. MotionEye is different in that it does NOT detect object, but note the difference between two frames. In a way it is much easier to implement. One last thing I notice the guy gave the MotionEye demo. I thought he was a very smart teacher because he did the following in the last few minutes. From standing position, he squashed down. It took me a couple of seconds to appreciate why he did not keep standing up earlier, … You might like to tell me why he did so, before I move on, … – tlfong01 14 hours ago
  • Sorry for all the typos, actually I was hopping among different newspaper and forums on different topics. So I might have confused you because I mixed you topic with others.:) – tlfong01 14 hours ago
  • I search my penzu diary to refresh my memory. Now I remember the funny guy making very professional video is called Christ Barnatt. I am a huge fan of him. I only recommend his video yesterday. raspberrypi.stackexchange.com/questions/106559/…. – tlfong01 14 hours ago
  • This is one of the perhaps more than 100, yes 100 answers I gave to explain the problem of low level trigger relays: raspberrypi.stackexchange.com/questions/99988/… – tlfong01 14 hours ago
  • And this is the first of my 500+ posts on KY019 relay and related topics, mainly on home automation, and follow up questions: raspberrypi.org/forums/viewtopic.php?f=37&t=77158#p1323061 – tlfong01 14 hours ago
  • I am not sure if my down/up votes ratio I mention is precise, so I check my record. This is a summary: DownVote 7 Votes cast. Upvote 2,394 Votes cast. A more accurate ration is 7/2394 == 0.3%! – tlfong01 13 hours ago
  • BTW, your relay problem is related to “Rpi low not low enough” This is one of perhaps 20+ answers I gave for the very same question which appears from time to time: raspberrypi.stackexchange.com/questions/83494/… – tlfong01 13 hours ago
  • Question: Are you following any of the below installation guides, wikis, and tutorials? Are there any better ones? (1) github.com/ccrisan/motioneye/wiki/Install-On-Raspbian (2) raspberrypi.org/forums/viewtopic.php?t=238708#p1458810 (3) github.com/ccrisan/motioneyeos/wiki (4) billw2.github.io/pikrellcam/pikrellcam.html. – tlfong01 13 hours ago
  • I actually was about to suggest the MotionEye docker installation. This way you don’t need to spend time to install python in the Rpi SD image which you mention that you wish to have more space in you SD card. I agree that SD card is a potential problem. I read that Rpi guys recommend not to use 32GB SD cards because they might cause trouble. One other reason I could not give an answer immediately is because I don’t know nothing about Bash scripts. I never write any programs in bas scripts. Almost all my programs in Rpi apps are python. So cards because they might cause trouble. – tlfong01 7 mins ago
  • One other reason I hesitate to give an answer is because I don’t know nothing about Bash scripts. I NEVER write any programs in Bash scripts. Almost all my programs in Rpi apps are in python. So if you install MotionEye docker in Rpi4B buster and translate your bash script to python, I can try to help almost immediately! ;). This reminds me another reason that I hesitate to answer is because the GitHub doc say only up to Rpi3 stretch is supported. I have been using Rpi4B buster for over 6 months and got addicted to it. I am very reluctant to go back to Rpii3B+ stretch just for MotionEye. – tlfong01 2 mins ago   Edit
  • I would rather wait for MotionEye to upgrade to support Rpi4B buster. I am not fluent in docker. You might like to give me docker related proj blogs and I am interested to try them out. – tlfong01 45 secs ago   Edit

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: