Arduino relay problems

The relay works, but it switches on and off at random. I wonder if it can be an issue with the sensorpins. Can the signal from the sensor get some interference if I put them next to a power wire? It is about 1,5 meters from the sensor to the arduino, and the signal wire and power wires run side by side allmost all the way.

Hi,
What does the monitor screen say, is the software or hardware causing the problem?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

If I only knew :grinning: I hooked it up to my computer so I could watch the monitor while runing, and for some reason the flow counter on one of the sensors did not stop when it reached 30 (as the set value). I suspect that it may be my wiring, because the program does not run the same way when the arduino is powered by the computer instead of the power supply.

This is my project by the way :joy:

Hi,
Thanks for the picture.
If you are using internal pullups on the flow meter outputs, they may be too weak and you are getting noise on the signal line.
You could add external 10K pullups as well and see if that helps.

Can you post link to your flow meter please?

Tom.... :grinning: :+1: :coffee: :australia:

Hi, it these flowmeters I’m using.

US $4.68 4% Off | Water Flow Sensor 0.3-6L/min Switch Meter Flowmeter Counter Sensor Water Control 1/4" 1.75MPa DIY Kit

It is a very bad idea to supply the Arduino UNO (or a Nano, Leonardo, Mega 2560, Pro Mini) with 12 V to the "barrel jack" or "Vin" as the regulator on board is not capable of supplying more than 100 mA or so and other devices may be drawing significant current towards this order.

Notwithstanding other advice about isolation - which you have not wired your relay modules in a manner to provide - you should use the same regulated 5 V supply powering all other parts, to power the UNO via the "5V" pin and ground, but you need to disconnect only the "5V" pin whenever you connect the USB to a PC for programming or supervision.

The major and clear problem with your lash-up is the unruly wiring, This can be a terrible source of problems. All connections between one circuit element and another must be bundled together, power with ground or control signal and ground or in fact, all three together as with your sensors or the relay modules where they are not implementing the opto-isolation.

Where you can use the opto-isolation on the relay modules (and you need to cite which modules you are using - the Website describing them if you want advice on how this is done) you then need to have the control wires including the return to ground or 5 V in one bundle and the relay power wire pair as a separate bundle connecting directly to the output terminals of the 5 V regulator.

It also should be reasonably obvious that the wiring switched by the relays must be kept clearly separate from all the logic wiring and obey the same rule - switched line bundled with its own "ground" return.

Why are you running the ESP from 7 V instead of 5? it looks as if the ground return from it to the UNO is taking a completely alternate meander. Asking for trouble! :astonished:

The logic flow in loop() is quite complex and the empty else{} cases are unnecessary and adding to the visual noise along with the lack of consistent indenting and spacing.

There is the if test of when teller ==2 that the loop is terminated with a return statement, leaving a great deal of unexecuted code and in the code is the only place I see the two outputs ol and ol2 set to LOW.

Overall, your program is lacking structure and clarity which occurs when you create a program “on the fly” without first having created an overall operational flow chart. It is one of the both beauties and curses of the Arduino platform.

Your program would benefit greatly with modifications which are unfortunately not trivial but would make it much easier to understand operation and far easier to modify and or change the operation to suite your needs.

  1. Implement program logic using a state machine. This breaks operation down into smaller, manageable tasks without the complex nesting of if-then-else code blocks.

  2. Using delay(), while easy to implement, creates a non-responsive system during the time that call is executing. Nothing else can happen. Even interrupts will be lost if the code to process them is in the users loop(). The solution is to remove all calls to delay() with the exception of short delays during the setup() task. This involves the usage of the millis() function so that code can continue to operate during long timing events, this keeps the system responsive to operator inputs at all times.

There are a multitude of these programming techniques covered on the web and in the Arduino documentation. If you want specifics, just ask.

I think you’ll continue to struggle with bugs that are both hard to find and even harder to fix until you simplify the logic flow of the system. Just my two cents worth of free advise.

Thank you for your reply. The 7v is for the nodemcu, and that is only used for sending a push notification via Blynk to my IPhone. The two wires from it is only ground through a button to input. And reset on the other side.

Thank you for the input. I have tried several times to use millis() instead of delay, but I just don’t understand it. I have been working on this project for a couple of years and tried to learn as I go, but I am about to give up.

Maybe it would be best to just pay someone to do it.

But the NodeMCU is supposed to be powered by 5 V. If it is the version 0.9 then as per another discussion here, 7 V is exceeding the rating of the USB interface chip. Why would you do that?

As I explained, the ground and the two control wires should be bundled together as they travel to the Arduino. And I wonder what level shifting arrangements you made to connect the Arduino's 5 V logic to the 3.3 V NodeMCU?

However, since the ground is common to the power supply as well, those wires should also be bundled to the power supply wires. The wiring layout is a tricky matter.

And - I am wondering what the relays are actually controlling - apparently something powered by the 12 V. If it is an inductive load, the wiring layout becomes particularly critical.

I’ll try to explain in my bad english what my project is about and what it’s supposed to do step by step.

As I mentioned it is an automatic bottle filler, that fills two bottles at a time.

First when I power up, a conveyor belt with all the bottles start moving. When two bottles have passed the IR-sensor (counted two bottles), the conveyor stops and a pneumatic arm pushes the two bottles forward. The two bottles is then pressing on a button as a safety in case the IR-sensor counts something and and the conveyor stops and the pneumatic arm is moving nothing forward. If the button is not pushed, the pneumatic arm retracts and the conveyor starts again. When the two bottles are in place a new pneumatic valve opens and lower the bottle filler in place on top of the bottles. It then opens a valve and pressurises the bottles. Then it opens the two valves and the liquid starts to flow into the bottles, when a set amount of liquid has passet through the flow sensors (one for each bottle) the valves closes and sits for a bit to slowly depressurise the bottles before another valve opens and let the remaining pressure out. Then the bottle filler rises and another pneumatic arm is activated and pushes the bottles sideways along a table. When the bottles stop, another pneumatic valve is opened and lowers the capping heads that crimps the caps on to the bottles. Then the capper then rises back up, and it starts all over with the conveyor belt starting.
I have also implemented a button for selecting between two different amount of liquid (330ml and 500ml). For now I just use 30ml and 50ml to save time while testing.

Thats about it, probably not easy to understand, but I did my best to explain.

HI,
Did you write your code in stages?
Have you got separate codes for each of you inputs and outputs to prove the integrity of each?

Make sure all your inputs and outputs work on their own reliably.
Then add them together one at a time to again make sure they work the way they do.
Make sure your measurement system works accurately then add it.

At what stage of your programming did the problems you have encountered occur?

Tom... :grinning: :+1: :coffee: :australia:

Hi, I did as you suggests. I put the code together bit by bit, and tested it as I went along. Everything worked fine on my workbench when I had it connected to my computer and used led diodes as substitutes for the relays. It was when I powered it with the 12v power supply and connected the relays the problems started.

Hi,
Have you checked ALL your ground connections.
Have you avoided daisy chaining your ground connections, especially having gnd running through your controller, instead of using JUST ONE gnd pin on the controller for I/O.

Using a STAR configuration of your gnd wiring and good gauge wire may help.

Tom..... :grinning: :+1: :coffee: :australia:

Hi,
7109641004f47224750161941fe97adcc52fc17eedit

Tom... :grinning: :+1: :coffee: :australia:

That is an automatic consequence of what I have been explaining about proper wiring. I was not sure about the problem described being due to the wiring and had not closely examined the code but looking more closely - a relay "flickering on and off" - could exactly be due to interference being coupled by the loose wiring in many different possible ways and would obviously not happen when only LEDs were used in simulation.

Can't say I can see where you mentioned that - or was it in your cross posting? :roll_eyes:


My recommendation, painful though it be, is that you sit down and **completely** re-wire it properly. Power the ESP with 5 V, not 7 and use the second regulator to separately power the relays at 5 V, re-wired to use the opto-isolators. To do that, you need to cite which modules they are as I cannot see them quite sufficiently clearly in your picture to figure out the proper connections.
Note the "**" showing in the text. This forum code is massively buggy! :astonished:
1 Like

Thank you guys for trying to help me. I will do as you recommend. I will rewire everything, and do the suggested changes. I will be back when it is done :joy:

Please note carefully what I explained in #27. Basically there should be only one bundle of wiring traceable as a path between any two modules, except for the opposite sides of a relay module. But please do cite the relay modules so I can advise on how to connect them. :grinning:

You have a 12V power supply, WHY are you using 5V relays? After stepping 12V down to 5, the puny voltage regulator on the Mega can only supply about 120mA before overheating and shutting down, 2 relay coils draw 150 mA.