element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum help with how to use signal from motorcycle hall effect sensor.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 3 replies
  • Subscribers 401 subscribers
  • Views 776 views
  • Users 0 members are here
  • source_code
Related

help with how to use signal from motorcycle hall effect sensor.

Former Member
Former Member over 12 years ago

need to hook into the hall effect sensor on my motorcycle for an experiment. I am thinking I need to just hook signal to a pin and ground to ground. Sound right?

 

Can I hook to a PWM digital pin?

 

The details for the speed sensor:

Speed sensor output voltage

When sensor is on

DC 4.8 V or more

When sensor is off

DC 0.6 V or less

 

new code with attachInterrupt, will it work?

 

I

  • Sign in to reply
  • Cancel
  • Former Member
    Former Member over 12 years ago

    Do you want to read the sensor or fake the sensor?
    In either case I doubt PWM will help you, because speed sensor most likely outputs different frequency signal depending on speed, while PWM on arduino has fixed frequency and only on-time of signal vs off-time is varied (duty cycle). Another thing to look for - what output type that sensor has? Since your motorcycle most likely has 12V system and sensor is said to output 4.8 or higher for high and 0.6 or less for low, based on what I have seen in speed sensors, it could be so called open collector output - that means that data pin is pulled high externaly and all that sensor does is pulls it down to ground when needed. You can check if that is the case by disconnecting sensor and checking if there is voltage present on data line coming from instrument panel. Anyway, before connecting arduino to your motorcycle, check carefully what voltages are present, because you can easily damage your arduino if you hit input pins with 12V.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago

    I would like to reference the following in my attempt to get this code figured out.

    "http://www.freetronics.com/pages/jaycar-water-flow-gauge"

     

    I do not know how to determine calibration number for my application and if this looks feasible for calculating rpm from the bike hall sensor (not motor rpm but just rather a constant), I still have to figure out how to incorporate it back into my main program.

     

    I took their code,

    #include <LiquidCrystal.h>
    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

     

    // Specify the pins for the two counter reset buttons and indicator LED
    byte resetButtonA = 11;
    byte resetButtonB = 12;
    byte statusLed = 13;

     

    byte sensorInterrupt = 0; // 0 = pin 2; 1 = pin 3
    byte sensorPin = 2;

     

    // The hall-effect flow sensor outputs approximately 4.5 pulses per second per
    // litre/minute of flow.
    float calibrationFactor = 4.5;

     

    volatile byte pulseCount;

     

    float flowRate;
    unsigned int flowMilliLitres;
    unsigned long totalMilliLitresA;
    unsigned long totalMilliLitresB;

     

    unsigned long oldTime;

     

    void setup()
    {
    lcd.begin(16, 2);
    lcd.setCursor(0, 0);
    lcd.print(" ");
    lcd.setCursor(0, 1);
    lcd.print(" ");

     

    // Initialize a serial connection for reporting values to the host
    Serial.begin(38400);

     

    // Set up the status LED line as an output
    pinMode(statusLed, OUTPUT);
    digitalWrite(statusLed, HIGH); // We have an active-low LED attached

     

    // Set up the pair of counter reset buttons and activate internal pull-up resistors
    pinMode(resetButtonA, INPUT);
    digitalWrite(resetButtonA, HIGH);
    pinMode(resetButtonB, INPUT);
    digitalWrite(resetButtonB, HIGH);

     

    pinMode(sensorPin, INPUT);
    digitalWrite(sensorPin, HIGH);

     

    pulseCount = 0;
    flowRate = 0.0;
    flowMilliLitres = 0;
    totalMilliLitresA = 0;
    totalMilliLitresB = 0;
    oldTime = 0;

     

    // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
    // Configured to trigger on a FALLING state change (transition from HIGH
    // state to LOW state)
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
    }

     

    /**
    * Main program loop
    */
    void loop()
    {
    if(digitalRead(resetButtonA) == LOW)
    {
    totalMilliLitresA = 0;
    lcd.setCursor(0, 1);
    lcd.print("0L ");
    }
    if(digitalRead(resetButtonB) == LOW)
    {
    totalMilliLitresB = 0;
    lcd.setCursor(8, 1);
    lcd.print("0L ");
    }

     

    if( (digitalRead(resetButtonA) == LOW) || (digitalRead(resetButtonB) == LOW) )
    {
    digitalWrite(statusLed, LOW);
    } else {
    digitalWrite(statusLed, HIGH);
    }

     

    if((millis() - oldTime) > 1000) // Only process counters once per second
    {
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);
    //lcd.setCursor(15, 0);
    //lcd.print("*");

     

    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

     

    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();

     

    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60) * 1000;

     

    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitresA += flowMilliLitres;
    totalMilliLitresB += flowMilliLitres;

     

    // During testing it can be useful to output the literal pulse count value so you
    // can compare that and the calculated flow rate against the data sheets for the
    // flow sensor. Uncomment the following two lines to display the count value.
    //Serial.print(pulseCount, DEC);
    //Serial.print(" ");

     

    // Write the calculated value to the serial port. Because we want to output a
    // floating point value and print() can't handle floats we have to do some trickery
    // to output the whole number part, then a decimal point, then the fractional part.
    unsigned int frac;

     

    // Print the flow rate for this second in litres / minute
    Serial.print(int(flowRate)); // Print the integer part of the variable
    Serial.print("."); // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (flowRate - int(flowRate)) * 10;
    Serial.print(frac, DEC) ; // Print the fractional part of the variable

     

    // Print the number of litres flowed in this second
    Serial.print(" "); // Output separator
    Serial.print(flowMilliLitres);

     

    // Print the cumulative total of litres flowed since starting
    Serial.print(" "); // Output separator
    Serial.print(totalMilliLitresA);
    Serial.print(" "); // Output separator
    Serial.println(totalMilliLitresB);

     

    lcd.setCursor(0, 0);
    lcd.print(" ");
    lcd.setCursor(0, 0);
    lcd.print("Flow: ");
    if(int(flowRate) < 10)
    {
    lcd.print(" ");
    }
    lcd.print((int)flowRate); // Print the integer part of the variable
    lcd.print('.'); // Print the decimal point
    lcd.print(frac, DEC) ; // Print the fractional part of the variable
    lcd.print(" L");
    lcd.print("/min");

     

    lcd.setCursor(0, 1);
    lcd.print(int(totalMilliLitresA / 1000));
    lcd.print("L");
    lcd.setCursor(8, 1);
    lcd.print(int(totalMilliLitresB / 1000));
    lcd.print("L");

     

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;

     

    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
    }
    }

     

    /**
    * Invoked by interrupt0 once per rotation of the hall-effect sensor. Interrupt
    * handlers should be kept as small as possible so they return quickly.
    */
    void pulseCounter()
    {
    // Increment the pulse counter
    pulseCount++;
    }

     

    image
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to Former Member

    and turned it into this.

     

    Code:

    // HOW WOULD I DETERMINE MY CALIBRATION FACTOR?
    float calibrationFactor = 4.5;

     

    volatile byte pulseCount;
    float rpmRate;

     

    unsigned int rpm;

     

    byte sensorInterrupt = 0; // 0 = pin 2; 1 = pin 3
    byte sensorPin = 2;

     

    unsigned long oldTime;

     


    void setup()
    {
    Serial.begin(9600);
    pinMode(sensorPin, INPUT);
    digitalWrite(sensorPin, HIGH);

     

    pulseCount = 0;
    rpmRate = 0.0;
    rpm = 0;
    oldTime = 0;

     


    // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
    // Configured to trigger on a FALLING state change (transition from HIGH
    // state to LOW state)
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

     

    }

     


    void loop()
    {

     


    if((millis() - oldTime) > 1000) // Only process counters once per second
    {
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);

     


    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    rpmRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

     

    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();

     

    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    rpm = (rpmRate / 60);

     

    // to output the whole number part, then a decimal point, then the fractional part.
    unsigned int frac;

     


    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (rpmRate - int(rpmRate)) * 10;

     


    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;

     

    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
    }
    }

     


    /**
    * Invoked by interrupt0 once per rotation of the hall-effect sensor. Interrupt
    * handlers should be kept as small as possible so they return quickly.
    */
    void pulseCounter()
    {
    // Increment the pulse counter
    pulseCount++;
    }

     

     

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube