I'm using Arduino pro mini (3.3V 8Mhz). I want detect if something is connected to any of my analog pins or not. I found an answer.
No you can't directly. However if you use a pull-down resistor of 10k to ground and then wire your sensor to the analog input pin, then any reading higher then 0 would imply that there is a sensor actively connected and supplying the reading obtained.
But what if I want to know which sensor is connected to which port?
For the 'which sensor' part, as I'll be designing the sensors on my own, I'm thinking of providing an additional wire with all the sensors having a unique fixed output voltage, which would then be connected to some analog pin. For example if there are 3 sensors (0,1&2 namely), I'll set 1 wire of sensor0 as 0.5V, sensor1 as 1V & sensor2 as 2V. Thus I can know which sensor it is, even if multiple sensors are connected. But how do I figure out which sensor is connected to which pin?
Not really. Yes, I know where I have connected which sensor, but the arduino doesn't! Using my method it will only know which sensor(s) are connected, but how does it figure out which sensor is connected to which pin if I connect multiple sensors?
Kunal_Gupta:
but the arduino doesn't! Using my method it will only know which sensor(s) are connected, but how does it figure out which sensor is connected to which pin if I connect multiple sensors?
Then you have to tell the Arduino...
This can only work if you define pairs of sensor data pin and sensor identification pins. For example A0 for sensor data and A1 for identification. They form sensor 1. For sensor 2 you use A2 and A3 etc.
It's impossibly to make that free to choose because the Arduino can't even distinguish sensor data voltages from a sensor identification voltages... You have to group that somehow.
For A0 to A5 you can turn on the internal pullup resistors with pinMode(A0, INPUT_PULLUP);
After that you can read the pin with analogRead(A0);
Then you can turn the internal pullup resistor off: pinMode(A0, INPUT);
This does not work for A6 and A7, they are analog inputs only and they don't have the digital I/O.
This can only work if you define pairs of sensor data pin and sensor identification pins. For example A0 for sensor data and A1 for identification. They form sensor 1. For sensor 2 you use A2 and A3 etc.
It's impossibly to make that free to choose because the Arduino can't even distinguish sensor data voltages from a sensor identification voltages... You have to group that somehow.
I did think of this way out. But the problem is that I don't have the luxury of using 2 analog pins for a each sensor. I need to interface 6 sensors atleast, and identity which sensor is connected to which pin.
But there are only 8 ADC channels.
Depending how quickly you want to read the sensor you may use digital interface instead. For example use a I2C ADC onboard with each sensor and read it via the external ADC - depending on address you will know which device is connected. Or you may emulate such functionality with another MCU. I can imagine a small ATTiny reading the sensor (preprocessing data if you want) and sending them via any interface you like to the main Arduino. Sensor type information will be encoded as I2C address or one byte of the data.
Kunal_Gupta:
I did think of this way out. But the problem is that I don't have the luxury of using 2 analog pins for a each sensor. I need to interface 6 sensors atleast, and identity which sensor is connected to which pin.
But there are only 8 ADC channels.
Is there no other way to do it?
You could use an Analog Multiplexer to give you 8 analog inputs which you can read one at a time with a single analog input.
Or...
If you are designing your own sensors, then you could add an analog multiplexer to each sensor.
The sensor would then have an analog output and a digital input. The digital input goes to the analog multiplexer to select the 'identify voltage' or the actual sensor output.
You then wire your sensors to Arduino analog inputs and one digital output common to all your sensors.
When you switch the digital output, it would switch all your sensors from 'Identify Voltage' to 'Sensor signal'
If you are designing your own sensors, then you could add an analog multiplexer to each sensor.
The sensor would then have an analog output and a digital input. The digital input goes to the analog multiplexer to select the 'identify voltage' or the actual sensor output.
This would solve my problem. But increase the cost drastically. Any other way?
Other way, do the identification digital. Just use 3 pins for each sensor and you can use a binary address. You then have the possibility to address 7 (+ 1 state for none connected). Just assign each sensor an address by pulling the pins high/low.
And you can even reduce the number of sensor wires to two is you use floating as a state as well. But distinguish floating pins from HIGH/LOW pins, just switch between a pull up and pull down resistor (last you have to add yourself on an Uno/Nano/Pro Mini). If a pin reads HIGH no matter if it's pulled up or down it's a high. If it read LOW no matter it's pulled high or low it's a low. And if it read low when pulled low and high when pulled high it's floating. That way you have 3 states per pin => 3 x 3 = 9 states = 8 addresses + both floating for no sensor connected.
Or you may use output impedance of the sensor as identification. Measure it with pull-up active and inactive and according to the difference you can say if it is high impedance source (large difference; i.e. 100k resistor at output of the sensor) or a low impedance source (i.e. output buffered via op amp; no difference in this case).
Or you may let various sensors be able to output only different voltage levels - i.e. sensor 1 able to output 0-0.5V, sensor 2 outputs 1-1.5V etc. Easy but resolution will suffer.
I am not sure if anyone suggested EEPROM solution - you can put I2C (or even 1-wire) EEPROM on the sensor and read it.
There is sooo many options and it depends what sensors you want to use, what performanse you expect, how much you are willing to pay and how much time you are willing to spend in creating the interface.
I think the need for fixed pin combinations is bad. From presented suggestions I like most mine (ofc) with standalone ATTiny digitizing the sensor and TonyWilk's with multiplexers. With multiplexers you have common contol lines and you switch all sensors from measuring output or indentification resistors. There are better multiplexers than 4051 for this, using them may reduce cost considerably (but again, it depends on what you need).
TonyWilk:
There are other analog switch parts, for example these are only $0.27 at 10-off: SN74LVC1G3157
If you think with the hobby hat on, 37 cents per unit is probably affordable (can't look in the OP's wallet )
Now think with a commercial hat Let's say that you sell 100,000 units; according to the page you linked, that is roughly $9000 extra costs that could be saved (if a solution without additional components can be found).
sterretje:
Now think with a commercial hat Let's say that you sell 100,000 units; according to the page you linked, that is roughly $9000 extra costs that could be saved (if a solution without additional components can be found).
Yeh, you have a point there.
However, it wouldn't take long to burn $9000 in dev. costs looking for that solution to save the $9000
I do have a solution that may save me all that money you're all taking about. But the solution would require quite an voltage divider, that too below 0.55 Volts. Any ideas on how I can step down 3.3V or 5V exactly to 0.1,0.2,0.3,0.4,0.5 and 0.6 separately?
I do have a solution that save all that money you're talking about. But that'd require quite an exact voltage divider. Any ideas on how to build a voltage divider that can step down 5V or 3.3V exactly to 0.1V, 0.2V, 0.3V, 0.4V, 0.5V & 0.6V. The arduino must not confuse among the 6 voltages i.e. it should be able distinguish between each of the voltages.
You need to either use analogRead(), which can differentiate to 0.00488V with Aref at 5V (default setting).
Or, run the voltage thru 6 analog comparators (see the autovoltage sense circuit with LM358 on an Uno, where 3.3V is compared against) and then read the output of the comparators as 6 digital inputs.
The EEPROM solution also seems good to me as they are quite cheap, cheaper than analog multiplexers. Can u please suggest how and which EEPROM would suitable for identification of 6 analog and 6 digital sensors?
For 12 sensors you need 4 bit of data. Any EEPROM will work - take the cheapest one with the right package and interface.
Or you may use PISO shift register like HC165 if it is more convenient.