I want to use an Arduino to control systems in my new workshop.
The Arduino Mega2560 has 16 analog pins and 54 digital pins. I wanted to use the analog pins as input from current sensors to let me know if current is flowing in a circuit. Then it should set the correspondingly numbered digital pin “high”.
But, there's a problem. The analog pins act like antennae ,so that their value is constantly floating. When I expect a 0 on an idle pin I get readings from 200 to 400. Useless in trying to determine if the current is off.
Suggestions:
1. Ground unused pins through a 10K ohm resistor . But I want to use them, they just won't always have current flowing. I could see doing that for those pins that I would never use, but then a don’t care what their state is anyway.
2. Use a virtual resistor by using the command pinMode("INPUT, PULL-UP") But that just changes the readings to between 1020 and 1022. Still not consistent and still useless.
3. Use input averaging. But what I want is not the average reading, but a 0 or 1 depending on whether the current is on or off.
I have thought of abandoning the analog pins altogether and using two groups of digital pins as they aren't adversely affected by electromagnetic interference. That means my programming won't be quite so elegant, but perhaps the only solution. I'd appreciate your thoughts.
If you want pull-up in analog mode :
When the microcontroller starts up, inputs A0-A16 are in digital mode, which means they have pull-ups built in.
With AVRs (Uno, Mega), the pull-up control is independent of the choice of digital or analog mode.
In analog mode, it is therefore possible to activate a pull-up.
Note: This is not possible with Arduino functions. Pinmode forces digital mode.
This is only possible using the registers that manage the GPIOs.
Reading the datasheet is mandatory.
Note: The pull-up value can vary from 30 to 50 kΩ depending on the manufacturing batch (see datasheet). This variation must not interfere with the sensor's response.
Get you project working in prototype on a breadboard, JUST connect one current sensor to the Mega and write code for that. To prove you have connections and code correct.
Develop your project and code in stages, first write code for the separated parts of your project.
Can you please tell us your electronics, programming, arduino, hardware experience?
Only when they are not connected to anything. Yours will be connected to current sensors, so what's the problem?
When no current is flowing, the sensor will pull the analog input to ground, so you should get readings close to zero.
Oh yes they are! If floating, they will read high or low at random.
Wire up some pots to the analog inputs to simulate the sensor input.
I'm still not really getting what the real problem is here. Will the sensors sometimes need to be disconnected, and you don't want the Arduino to react unpredictably? If that's it, you could use jack plugs & sockets. Some sockets have additional terminals (usually marked "switch") that are isolated when the plug is inserted, but when the plug is disconnected, this terminal shorts to the terminal for the tip. You can connect a 10K from that "switch" terminal to ground. That way, when the plug is disconnected, the 10K will pull the analog input to ground.
Further to this, if Wokwi doesn't grab you, the Serial Monitor built into the IDE is fully capable of allowing you to input signal values/set/clear digital inputs. It's all in how you write the software. Simple settings/clearings can be done with an alpha character - A for input 1 on, a for input 1 off. Analog signals take a bit more work, but it's well within the capabilities of even a novice, with a bit of guidance.
Granted, if you want to exercise every input on a Mega, it'll be a lot of work, but I'd go at it piecemeal. Focus on one or two sensors, and their related inputs and outputs, get it all working, then move on.
I'd still recommend Wokwi over SM, but it's your problem, so it has to work for you.