Multiple sensors to 1 pin

can I define multiple analog reads from 1 pin? I am using an attiny85 and planning on connecting a hall sensor that will be swappable for a reed switch and a regular button all made to be swappable with a simple wire disconnect system (don't know how to explain that part hope u understand). I am very new to this and was hoping I'd get some good help from this great community thanks again

Are you wanting to plug various sensors into one socket and for the code to recognise what sensor is connected?

you need to multiplex, somehow and that means additional lines to select the sensor.
Or you must make an external something that rotates automatically the addresses, but then the tiny has to find out what to read.

Is I2C an option?

robtillaart:
Is I2C an option?

I don't think I²C is an option as the requirement seems to be to use only one analogue pin. Also I don't think there is a need to multiplex as sensors will be "swappable with a simple wire disconnect system" whatever that means.

Archibald:
Are you wanting to plug various sensors into one socket and for the code to recognise what sensor is connected?

Exactly what you said.
Basically this is my project in a nutshell.
I programmed two attiny85s with arduino they're connected via 433mhz
I want to be able to connect various sensors to one socket like you said for example a tilt switch Then maybe a Hall effect sensor then maybe like a regular button and have them all on the same male/female plug in. Then I want the attiny85 to be able to do if statement like if tilt switch is tilted then send data to rxattiny and turn on led(which I am doing using Manchester and it's working but I'm just sending data to turn on led) but then unplug the tilt switch and on the same input I want to be able to read the Hall effect sensor and send the same data and the same with the button etc. my problem is I know that you have to define every pin but won't it clash if u define a pin more than once? I know it can be done but my limited arduino knowledge makes it difficult however I'm trying to learn. And if so is there any example of someone doing this? So I could study the code? Thank you guys for helping out

There is this option to make a resistor ladder on one analog port, to serve many on/off switches. Every switch gives another value, but there are limits e.g. pressing 2 buttons simultaneously can be ambiguous.

This is the well written tutorial of John Boxall you are looking for

(should be in your Arduino bookmarks)

robtillaart:
There is this option to make a resistor ladder on one analog port, to serve many on/off switches. Every switch gives another value, but there are limits e.g. pressing 2 buttons simultaneously can be ambiguous.

This is the well written tutorial of John Boxall you are looking for

(should be in your Arduino bookmarks)

This I am aware of and thank you for replying but what I want is different. I don't want them all plugged in simultaneously I would like to be able to switch them out at a moments notice individually without modifying code and on the same Input.

A hall effect sensor requires power so you will need a plug with at least 3 pins: ground, signal and +5V..

robtillaart:
There is this option to make a resistor ladder on one analog port, to serve many on/off switches. Every switch gives another value, but there are limits e.g. pressing 2 buttons simultaneously can be ambiguous.

This is the well written tutorial of John Boxall you are looking for

I guess the concept of using an analogue input for multiple buttons could be extended to allow sensors with analogue output signals. You could have a scheme something like this for the signal voltage:

Between 0.0V and 1.0V . . . . . hall effect current sensor with analogue output;
Nominally 1.5V and 2.0V . . . . tilt switch not-operated and operated;
Nominally 2.5V and 3.0V . . . . button not-operated and operated.

So if your Arduino reads say 2.06V, it can tell that's an operated tilt switch. I think that can be done by just the addition of three resistors with each sensor. A ladder resistor network is not applicable because the various sensors will be plugged in and out of one socket. Alternatively, if you are allowed a plug with more than 3 pins, you can use the extra pins to tell the Arduino what is connected.

A small python script that calculates the results of a cascaded voltage divider for 3 switches.
This configuration can identify simultaneous presses of switches.

#
# FILENAME: resistorladder.py
#   AUTHOR: rob van den tillaart
#     DATE: 2015-10-10
#  PURPOSE: script to simulate resistor ladder
#
# VCC ---[RVCC]-----[R1]-----[R2]-----[R3]---AR---[RGND]--- GND
#                  +-S1-+   +-S2-+   +-S3-+
# 
# AR is measurepoint AnalogRead
# switches are configured so it can switch 
# off a resistor in the voltage divider
#


# configurable items
resistor = [47, 33, 22]
RGND = 10
RVCC = 10
VCC = 5.0
MAXADC = 1023.0

d = {}

def result_resistor(switch):
    r = RVCC
    for i in range(len(resistor)):
        if switch[i] == 0:
            r += resistor[i]
    return r


def r_to_voltage(r):
    return VCC * RGND / (r + RGND)


def analogRead(voltage):
    return round(voltage * MAXADC / VCC)


if __name__ == "__main__":
    for k in range(8):
        s = [0, 0, 0]
        for i in range(3):
            if k & (1 << i) > 0:
                s[i] = 1
        rr = result_resistor(s)
        v = r_to_voltage(rr)
        ar = analogRead(v)
        # print s, '\t', rr, '\t', v, '\t', ar
        d[ar] = s

    for k in sorted(d):
         print k, '\t', d[k]

result, first column = analogRead expected value, second = the state of the switches

84.0    [0, 0, 0]
102.0   [0, 0, 1]
115.0   [0, 1, 0]
136.0   [1, 0, 0]
153.0   [0, 1, 1]
193.0   [1, 0, 1]
244.0   [1, 1, 0]
512.0   [1, 1, 1]

I have to think how this scheme can be extended with "pluggable" analog sensors.

The above scheme uses (analogRead) values in the range 70.. 550,
different values (and order) for the resistors would result in other ranges.

so you can replace this ladder with a sensor that use the range between 10..70 or above 550
however there must always be the 10K to GND ==> analogRead return 0 ==> no sensor

robtillaart:
I have to think how this scheme can be extended with "pluggable" analog sensors.

I don't think you can use a resistor ladder with "pluggable" sensors.

This is what I was thinking of for the tilt switch, giving 1.5V and 2.0V signal:

your tilt switch looks OK, but when you remove the tilt switch the 1.5K should still be connected to the signal pin to unambiguously see 0 ==> no devices.

So removing the tilt switch sensor would remove the switch the 6.3K and the 3.5K.

robtillaart:
your tilt switch looks OK, but when you remove the tilt switch the 1.5K should still be connected to the signal pin to unambiguously see 0 ==> no devices.

So removing the tilt switch sensor would remove the switch the 6.3K and the 3.5K.

It's a very good point that we need to detect when no sensor is connected.

Yes, moving the 1.5kΩ so it is permanently connected between the Arduino's analogue input pin and ground would make good sense. However that could provide too much load on some sensors so a higher value may be advisable.

In a previous post, I suggested using voltages between 0V and 1V for the analogue output of a hall effect sensor. If you move the 1.5kΩ resistor as described, you would need to reserve a difference range of voltages for that sensor. Although the sensor is analogue, a similar resistor network could be used to give the desired voltage range.

you can also have a persistant resistor between the 5V and signal, would free up the lower voltages for sensors.

All this is kind of confusing. :confused:
However upon reading your answers multiple times it's starting to make sense. How's this for an idea, how about I solder resistors to each sensor that vary in resistance so when they are connected to the Attiny it can read the value and know what it's using. As for defining pins I can define 1 pin to various sensors?

Wesley5n1p35:
All this is kind of confusing. :confused:
However upon reading your answers multiple times it's starting to make sense. How's this for an idea, how about I solder resistors to each sensor that vary in resistance so when they are connected to the Attiny it can read the value and know what it's using. As for defining pins I can define 1 pin to various sensors?

The resistors in my circuit diagram above will give 1.5V or 2.0V, depending on whether the tilt switch is closed. You can use other resistor values with a pushbutton so you get (say) 2.5V or 3.0V depending on whether the pushbutton is depressed:

That way your single signal pin will be able to determine which sensor is connected and what its state is. I hope that makes sense. This suggestion could work with analogue as well as digital sensors.

Is there any sample code you could write up because no one has answered my (define pin) question. Thanks again for everyone's help :slight_smile:

Wesley5n1p35:
Is there any sample code you could write up because no one has answered my (define pin) question. Thanks again for everyone's help :slight_smile:

Within the Arduino software enviroment, take a look at 'File' > 'Examples' > '01.Basics' > 'AnalogReadVoltage'.

If you follow roughly the scheme that has been suggested in posts above, instead of printing out the voltage value, you would use "if" statements to determine which sensor is connected. If an analogue sensor is connected, you would then wish to convert the voltage reading to whatever the sensor is measuring. So if it's a hall effect current sensor, you would want to work out the current in amps. If a digital sensor is connected you would need to determine whether it's in a 'low' or 'high' state (e.g. button not operated or button operated). If a digital sensor is produces say 1.5V for 'low' state and 2.0V, for 'high' state, your "if" statements would need to allow for some variation from these nominal voltages. So perhaps for a tilt switch, any voltage between 1.75V and 2.25V would be interpreted as 'operated':
if( voltage>1.75 && voltage<2.25 ) { ......... } // tilt switch operated