ArduinoHallEffectDebug.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. INSTRUCTIONS
  3. ============
  4. *** *** *** For testing the hall effect sensor and correct magnet polarity *** *** ***
  5. Connect the signal pin of the sensor to pin 2 on the Arduino (an interrupt pin). Connect the
  6. Ground to Gnd and Vcc to 5V. Set the detect_on variable to true. Upload the code to your Arduino.
  7. Open the serial monitor and set the Baud rate to 9600. All being well, when you bring the
  8. magnet close to the sensor, with the correct polarity, the serial monitor will print ‘detect’.
  9. *** *** *** To output hall effect RPM to a graph *** *** ***
  10. Connect the signal pin of the sensor to pin 2 on the Arduino (an interrupt pin). Connect the
  11. Ground to Gnd and Vcc to 5V. Set the detect_on variable to false. Set the number of magnets
  12. on the shaft using n_magnets. Upload the code to your Arduino. Open the serial plotter. When
  13. you spin the shaft with magnet(s) attached the RPM will be plotted.
  14. *** *** *** To output commutation RPM to a graph *** *** ***
  15. Connect up the commutation RPM sensor to the motor as per the manufacturers instructions.
  16. Connect the signal pin of the sensor to pin 2 on the Arduino (an interrupt pin). Connect the
  17. Ground to Gnd and Vcc to 5V. Set the detect_on variable to false. Set the number of poles in
  18. the motor using n_magnets. Upload the code to your Arduino. Open the serial plotter. When
  19. you power the shaft with magnet(s) attached the RPM will be plotted.
  20. */
  21. //Conversion factors
  22. #define MILLIS_TO_SEC 1000.0f
  23. #define SEC_TO_MIN 60.0f
  24. /*
  25. If you want to use the hall effect sensor to print 'detect' to the serial monitor when the magnet is
  26. near and the correct polarity is presented, set this to true. If you want to output an rpm value
  27. set this to false.
  28. */
  29. bool detect_on = true;
  30. /* Set the number of magnets on autorotation wheel / shaft or the number of poles on the motor,
  31. depending on the type of sensor being used. */
  32. int n_magnets = 1;
  33. volatile byte n_rev;
  34. float rpm;
  35. float prev_time;
  36. void setup()
  37. {
  38. Serial.begin(9600);
  39. attachInterrupt(0, detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
  40. n_rev = 0;
  41. rpm = 0.0f;
  42. prev_time = 0.0f;
  43. }
  44. void loop()//Measure RPM
  45. {
  46. //Only do rpm calculation after 20 rotations has past.
  47. if (n_rev >= 20) {
  48. rpm = SEC_TO_MIN*MILLIS_TO_SEC/(millis() - prev_time)*n_rev/n_magnets;
  49. //reset prev_time to now
  50. prev_time = millis();
  51. //reset rotation counter
  52. n_rev = 0;
  53. //print rpm value to serial console
  54. Serial.println(rpm,DEC);
  55. }
  56. }
  57. //This function is called whenever a magnet/interrupt is detected by the arduino
  58. void detect()
  59. {
  60. n_rev++;
  61. if (detect_on == true) {
  62. Serial.println("Magnet Detected");
  63. }
  64. }