Go Down

Topic: Hall Sensor and Power Switchtail (Read 208 times) previous topic - next topic

kvj477

Hello all,

I am using a Latching Hall Sensor for the first time. Working with a Power Switchtail to power a computer speaker to have audio play when a magnet attached to a bike wheel passes by. Having a few problems. I would like the speaker to start OFF, turn on when the magnet goes by, and then be able to turn off with some inactivity. I figured it would be easiest to use a delay to have the loop re-run, checking again. The delay isn't working. What code do I need to add? Here is my code so far:



/**
 * Switch the status Switchtail on when a magnet is nearby
 * reset the arduino after 10 seconds to allow sensor for inactivity.
 */
 
#define PIN_SENSOR_OUTPUT      2
//hall sensor input 2
#define PIN_STATUS             13
//the Switchtail is connected to 13
 
void setup()
{
  pinMode(PIN_SENSOR_OUTPUT, INPUT);
  digitalWrite(PIN_SENSOR_OUTPUT, HIGH); 
 
  Serial.begin(9600);
 
  pinMode(PIN_STATUS, OUTPUT);
 
}
 
void loop()
{
  digitalWrite(PIN_STATUS, 0);
  //switchtail starts off
  int hall_effect_sensor_reading;
 
  // 1 = no magnet present
  // 0 = magnet detected
  hall_effect_sensor_reading = digitalRead(PIN_SENSOR_OUTPUT);
 
  // TURN OFF or ON the switchtail
  if( hall_effect_sensor_reading == 1 ) {
    digitalWrite(PIN_STATUS, 0);
  } else {
    // magnet detected!
    // turn on LED
    digitalWrite(PIN_STATUS, 1);
    //I think this will turn off the LED and switchtail, but need to test.
    //Does LED need to change to an integer?
  }
 
}

Paulcet

A latching Hall effect sensor will not switch states unless the magnet polarity changes.  Your application will be better with a regular Hall effect sensor.

kvj477

But would that make my sound oscillate? I don't want it to waiver in and out of power/strength. Can you recommend exactly which sensor you recommend? I am new to using them. THANK YOU!

cattledog

Quote
But would that make my sound oscillate?
The way to avoid this is to write the code so that the presence of pulses turns the output on, and only turn it off when there are no new pulses within some period of time.

Paulcet is correct that the latching sensors are wrong for the task.  I believe that the factor which will drive your selection of a standard hall sensor is how you intend to mount it and what length of wires you need to run. You can use basic sensor like the Allegro A3144 if you can work out the mounting. I have successfully used the Hamlin 55140 and 55100 in some bicycle wheel projects. Google around and find something that meets your mechanical configuration.

kvj477

The Hamlin 55100 is working great at turning on and off the sensor! Thank you! I'm playing now with pulses, and wondering:

Quote
write the code so that the presence of pulses turns the output on, and only turn it off when there are no new pulses within some period of time.
Do you mean PWM? If so, I can't seem to figure out how to code it. Here again is my code without a pulse. Any help would be appreciated:

/**
 * Switch the status LED/switchtail on when a magnet is nearby
 */
 
#define PIN_SENSOR_OUTPUT      3
#define PIN_STATUS             13
//the Switchtail is connected to 13
 
void setup()
{
  pinMode(PIN_SENSOR_OUTPUT, INPUT);
  digitalWrite(PIN_SENSOR_OUTPUT, HIGH); 
 
  Serial.begin(9600);
 
  pinMode(PIN_STATUS, OUTPUT);
 
}
 
void loop()
{
  int hall_effect_sensor_reading;
 
  // 1 = no magnet present
  // 0 = magnet detected
  hall_effect_sensor_reading = digitalRead(PIN_SENSOR_OUTPUT);
 
  // TURN OFF or ON the LED
  if( hall_effect_sensor_reading == 1 ) {
    digitalWrite(PIN_STATUS, 0);
  } else {
    // magnet detected!
    // turn on LED
    digitalWrite(PIN_STATUS, 1);
    delayMicroseconds(100);
  }
 
}

cattledog

Quote
Do you mean PWM?
No.

You need to think this through and decide on your logic. First describe to yourself what you want the code to do. When do you want the switchtail to turn on? When do you want it to turn off?

Is there a wheel speed, i.e. some number of counts in a given period of time, when you want to turn it on?   Do you want to turn it off if there are no new pulses sensed in some different period of time?

Take a look at the example "blink without delay" in the digital examples of the IDE. As you can use this technique to define a period of time.

Decide what you want to do, try coding it, and if it doesn't do what you want it to do, come back, describe your problem, and post the code within the code tags which are found in the tool bar icon which is a scroll with <>.


kvj477

Thanks, Cattledog, I am still learning how to use this forum. I appreciate your patience. I took a stab at it, and it turns my switchtail on and off in this sketch, but the switchtail does not stay on. Any help appreciated.
Code: [Select]
/**
 * Switch the status LED/switchtail on when a magnet is nearby
 */
 
#define PIN_SENSOR_OUTPUT      3
#define PIN_STATUS             13
//the Switchtail is connected to 13
unsigned long time_of_last_magnet_detection;

void setup()
{
  pinMode(PIN_SENSOR_OUTPUT, INPUT);
  digitalWrite(PIN_SENSOR_OUTPUT, HIGH); 
 
  Serial.begin(9600);
 
  pinMode(PIN_STATUS, OUTPUT);
  time_of_last_magnet_detection = millis();
}
void loop()
{
  int hall_effect_sensor_reading;
  unsigned long current_time; 
  // 1 = no magnet present
  // 0 = magnet detected
  hall_effect_sensor_reading = digitalRead(PIN_SENSOR_OUTPUT);
  current_time = millis();
  // TURN OFF or ON the LED
  if( hall_effect_sensor_reading == 1 ) {
    // magnet not detected this loop.
if( (current_time - time_of_last_magnet_detection) > 2000 ) {
  // it has been more than 2000 ms (2s) time to turn off
  digitalWrite(PIN_STATUS, 0);
}
// if it hasn't been 2s yet don't change anything just let the LED remain on.

  } else {
    //magnet detected!
   
   //record new magnet detection time overwriting the old.
   time_of_last_magnet_detection = millis();
       // turn on LED
    digitalWrite(PIN_STATUS, 1);

  }
 
}

cattledog

#7
Feb 07, 2015, 04:53 am Last Edit: Feb 07, 2015, 08:20 am by cattledog
I'm going to change direction on you and raise the level of complexity, and I want to apologize for dragging this out, but I wanted to encourage to get as far as you could on your own.

The best way to be reading the hall sensor is with interrupts, instead of polling the status with digitalRead(). Take a look at this tutorial http://www.gammon.com.au/forum/?id=11488

The magnet is in front of the sensor for only a short time during the cycle of the wheel, and you need to make sure that you indeed see it as it goes by the sensor.

Here is some code which counts the number of times the magnet goes by the sensor in 5 seconds and then has a conditional test on that count to see what to do with the power. You can change the time period and the threshold counts for when you want to turn the power on and off. This example can also serve as the base for code which reads rpm or speed.

I have used external interrupt 1 which is on pin 3 which you are currently using. Depending on how you have wired the hall sensor, you may get cleaner readings if to change the trigger signal which the interrupt responds to by changing from RISING to FALLING.

Code: [Select]

volatile unsigned int  count = 0;//declare volatile because changed within interrupt

unsigned int copyCount = 0;
int threshold = 1;

unsigned long lastRead = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("start...");

  attachInterrupt(1, isrCount, RISING); //interrupt signal to pin3, FALLING is also possible
}

void loop()
{
  if (millis() - lastRead >=5000) //read interrupt count every 5 seconds
  {
    lastRead = millis();
    // disable interrupts,make copy of count,reenable interrupts
    detachInterrupt(1);
    copyCount = count;
    count = 0;
    attachInterrupt(1,isrCount, RISING);

    Serial.println(copyCount);//debug print
  }
  if (copyCount > threshold){
    //turn the power on
    //do other things while the wheel is turning
  }
 
  else{
    //turn the power off
    //do other things while the wheel is not turning
  }
}

void isrCount()
{
  count++;
}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy