Multiple analogue inputs controlling specific outputs

Hi there and apologies if this is on the wrong forum.

I’m currently working on a small project to sense multiple magnets and turn on LED’s based on their presence.

I have 4 analogue hall effect sensors set up so when they detect a change in the magnetic field they turn on an LED (there is one for each sensor), and turn it off again when the magnet is removed.

I’m currently doing this using a series of if/else if statements but I’d like to find a way where I can get multiple magnets to cause multiple LED’s to light up simultaneously and then execute commands based on that. As using the if statements means, if i add a delay etc. it will run that code before it turns on another LED.

I know I could do this by adding in more else if commands but it would turn into a factorial number as the more LED’s and hall effects I add the more if statements i need.

I.e
If(1,2,3,4)
If (1,2,3)
If (1,2,4) etc.

Also the ability to scale this to have multiple hall effects linked to one LED would be useful (again i can do this using && conditions in the if statement but i don’t think this is the best way to do it)

I hope this was clear enough, and thanks for your help!

HE data sheet - KY-035 Datasheet PDF - Datasheet4U.com

 int HEPins[] = {A1, A2, A3, A4};
int pinCount = 4;
int ledPins[] = {10, 11, 12, 13};

int val[4];
int valT[4];
int Val[4];

void setup() {
  Serial.begin(9600);           //  setup serial
  
  pinMode(ledPins[0], OUTPUT);
  pinMode(ledPins[1], OUTPUT);
  pinMode(ledPins[2], OUTPUT);
  pinMode(ledPins[3], OUTPUT);
  
    for (int i = 0; i < pinCount; i++) {
    // turn the pin on:
    valT[i] = (analogRead(HEPins[i]));
  }

}

void loop() {
  for (int i = 0; i < pinCount; i++) {
    // turn the pin on:
    val[i] = (analogRead(HEPins[i]));
    Val[i] = val[i] - valT[i];
  }
  
    Serial.print(Val[0]);
    Serial.print("\t");
    Serial.print(Val[1]);
    Serial.print("\t");
    Serial.print(Val[2]);
    Serial.print("\t");
    Serial.println(Val[3]);

  if (val[0] > 520) {
    digitalWrite(ledPins[3], HIGH);
  }
  else if (val[1] > 520) {
    digitalWrite(ledPins[2], HIGH);
  }
  else if (val[2] > 520) {
    digitalWrite(ledPins[1], HIGH);
  }
  else if (val[3] > 530) {
    digitalWrite(ledPins[0], HIGH);
  }
  else {
    digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
  }
}
[code]

You wrote: I have 4 analogue hall effect sensors set up so when they detect a change in the magnetic field they turn on an LED (there is one for each sensor), and turn it off again when the magnet is removed.".

Have you tested your premise that you can use an analog pin to detect what you want? What percent of change indicates a magnet has approached the sensor? What value are you looking for too determine the magnet has been removed?

And how close together are your sensors? Will a magnet on one sensor affect an adjacent sensor?

Paul

consider (tested with button switches)

it completes your use of for loops to used arrays by changing the order of the LED pins to match their association with the HEpins, and reducing the code to a generic operation for each HEpin

#define ON  LOW
#define OFF HIGH

byte HEPins[]  = {A1, A2, A3, A4};
byte ledPins[] = {13, 12, 11, 10};

int pinCount  = sizeof (HEPins);

static int valT[4];

char s [80];

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);           //  setup serial

    for (int i = 0; i < pinCount; i++)  {
        pinMode (ledPins[i], OUTPUT);
        pinMode (HEPins[i],  INPUT_PULLUP);
        valT[i] = (analogRead (HEPins[i]));
    }
}

#define HE_THRESH   520

void loop () {
    for (int i = 0; i < pinCount; i++) {
        int val = analogRead (HEPins[i]);

        if (HE_THRESH < val)
            digitalWrite (ledPins[i], ON);
        else
            digitalWrite (ledPins[i], OFF);
        
        sprintf (s, " %d %4d,", i, val);
        Serial.print (s);
    }
    Serial.println ();
}

Hi Paul,

I've got a basic version of the circuit set up on a breadboard and found that the analogue pins returns 500ish (i think this is just an integer value?) when no magnet is there and goes up by 40-150 depending on the distance of the magnet from the sensor, so 8-30% of the total reading.

The sensors are about 50mm (2inchs) apart and don't seem to be sensitive enough to pick up a reading from the adjacent ones!

Hi Greg,

That worked on the circuit I have set up and seems much neater thank you!

Why are you setting the internal pullups on the analog inputs? Post a link to those HES datasheets.

JCA34F:
Why are you setting the internal pullups on the analog inputs? Post a link to those HES datasheets.

i should have mentioned that I tested the code with buttons and needed the pullups