Components and supplies
Arduino UNO
Resistor 1k ohm
2-lines, 16 characters LCD display
trimmer 1-10 kOhm
Reed Switch from a bicycle speedometer
Project description
Code
Source code
arduino
1/* 2 Arduino Speedometer 3 by Cesare Brizio | CC by-sa-nc 4 this sketch is in the public domain 5 6 Version 1.0 - 26 february 2019 7 ------------------------------ 8 The implementation is relatively rough: 9 - wheel circumference has to be hard-coded 10 - an attempt at debouncing has been made both at ohysical and software 11 level - to no avail. Will be revised soon. 12 A few more details here: 13 http://www.cesarebrizio.it/Arduino/Speedometer.html 14 15 Modified from an original by John Boxall 16 Example 37.3 – Basic speedometer using millis(); 17 http://tronixstuff.com/tutorials > chapter 37 18 John Boxall | CC by-sa-nc 19 20 Hardware – you will need a sensor. For example – a reed switch and magnet. 21 Consider the reed switch to be a normally-open button, and connect as usual 22 with a pull-down resistor. 23 24 ========================== 25 The circuit for LCD board 26 (HD4770 compatible display) 27 4-wire dialogue 28 ========================== 29 * LCD RS pin to digital pin 15 30 * LCD Enable pin to digital pin 14 31 * LCD D4 pin to digital pin 5 32 * LCD D5 pin to digital pin 4 33 * LCD D6 pin to digital pin 3 34 * LCD D7 pin to digital pin 2 35 * LCD R/W pin to ground 36 * LCD VSS pin to ground 37 * LCD VCC pin to 5V 38 * 10K resistor: 39 * ends to +5V and ground 40 * wiper to LCD VO pin (pin 3) 41 42*/ 43 44// include the library code: 45#include <LiquidCrystal.h> 46 47// initialize the library with the numbers of the interface pins 48const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8; 49LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 50 51float start, finished; 52float elapsed, time; 53float circMetric=2.093; // wheel circumference (in meters) 54float circImperial; // using 1 kilometer = 0.621371192 miles 55float speedk, speedm; // holds calculated speed vales in metric and imperial 56 57void setup() 58{ 59 // convert metric to imperial for MPH calculations 60 circImperial=circMetric*.62137; 61 62 // the syntax with digitalPinToInterrupt should allow portability 63 //among different Arduino models - see https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ 64 attachInterrupt(digitalPinToInterrupt(2), speedCalc, RISING); // interrupt called when sensors sends digital 2 high (every wheel rotation) 65 //attachInterrupt(0, speedCalc, RISING); // interrupt called when sensors sends digital 2 high (every wheel rotation) 66 67 //start now (it will be reset by the interrupt after calculating revolution time) 68 start=millis(); 69 70 // set up the LCD's number of columns and rows: 71 lcd.begin(16, 2); 72 73 // Print a transitory message to the LCD. 74 lcd.print("Ardu-Speedometer"); 75 delay(2000); //just to allow you to read the initialization message 76 77 // top serial dialogue speed improves precision 78 Serial.begin(115200); 79} 80 81void speedCalc() 82{ 83 //Function called by the interrupt 84 85 if((millis()-start)>100) // 100 millisec debounce 86 { 87 //calculate elapsed 88 elapsed=millis()-start; 89 90 //reset start 91 start=millis(); 92 93 //calculate speed in km/h 94 speedk=(3600*circMetric)/elapsed; 95 96 //calculate speed in mph 97 speedm=(3600*circImperial)/elapsed; 98 } 99} 100 101void loop() 102{ 103 // The loop will be interrupted by the sensor each time the 104 // magnet passes near the sensor, in other words once per revolution 105 106 // Top line in the 16 char, 2 lines display - speed data 107 lcd.setCursor(0,0); 108 lcd.print(" "); 109 lcd.setCursor(0,0); 110 lcd.print(int(speedk)); 111 lcd.print(" km/h "); 112 lcd.print(int(speedm)); 113 lcd.print(" MPH "); 114 115 //bottom line the 16 char, 2 lines display - time data 116 lcd.setCursor(0,1); 117 lcd.print(" "); 118 lcd.setCursor(0,1); 119 lcd.print(int(elapsed)); 120 lcd.print(" ms/rev "); 121 122 // adjust for personal preference to minimise flicker 123 //delay(250); 124}
Source code
arduino
1/* 2 Arduino Speedometer 3 by Cesare Brizio | CC by-sa-nc 4 this sketch is in the public domain 5 6 Version 1.0 - 26 february 2019 7 ------------------------------ 8 The implementation is relatively rough: 9 - wheel circumference has to be hard-coded 10 - an attempt at debouncing has been made both at ohysical and software 11 level - to no avail. Will be revised soon. 12 A few more details here: 13 http://www.cesarebrizio.it/Arduino/Speedometer.html 14 15 Modified from an original by John Boxall 16 Example 37.3 – Basic speedometer using millis(); 17 http://tronixstuff.com/tutorials > chapter 37 18 John Boxall | CC by-sa-nc 19 20 Hardware – you will need a sensor. For example – a reed switch and magnet. 21 Consider the reed switch to be a normally-open button, and connect as usual 22 with a pull-down resistor. 23 24 ========================== 25 The circuit for LCD board 26 (HD4770 compatible display) 27 4-wire dialogue 28 ========================== 29 * LCD RS pin to digital pin 15 30 * LCD Enable pin to digital pin 14 31 * LCD D4 pin to digital pin 5 32 * LCD D5 pin to digital pin 4 33 * LCD D6 pin to digital pin 3 34 * LCD D7 pin to digital pin 2 35 * LCD R/W pin to ground 36 * LCD VSS pin to ground 37 * LCD VCC pin to 5V 38 * 10K resistor: 39 * ends to +5V and ground 40 * wiper to LCD VO pin (pin 3) 41 42*/ 43 44// include the library code: 45#include <LiquidCrystal.h> 46 47// initialize the library with the numbers of the interface pins 48const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8; 49LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 50 51float start, finished; 52float elapsed, time; 53float circMetric=2.093; // wheel circumference (in meters) 54float circImperial; // using 1 kilometer = 0.621371192 miles 55float speedk, speedm; // holds calculated speed vales in metric and imperial 56 57void setup() 58{ 59 // convert metric to imperial for MPH calculations 60 circImperial=circMetric*.62137; 61 62 // the syntax with digitalPinToInterrupt should allow portability 63 //among different Arduino models - see https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/ 64 attachInterrupt(digitalPinToInterrupt(2), speedCalc, RISING); // interrupt called when sensors sends digital 2 high (every wheel rotation) 65 //attachInterrupt(0, speedCalc, RISING); // interrupt called when sensors sends digital 2 high (every wheel rotation) 66 67 //start now (it will be reset by the interrupt after calculating revolution time) 68 start=millis(); 69 70 // set up the LCD's number of columns and rows: 71 lcd.begin(16, 2); 72 73 // Print a transitory message to the LCD. 74 lcd.print("Ardu-Speedometer"); 75 delay(2000); //just to allow you to read the initialization message 76 77 // top serial dialogue speed improves precision 78 Serial.begin(115200); 79} 80 81void speedCalc() 82{ 83 //Function called by the interrupt 84 85 if((millis()-start)>100) // 100 millisec debounce 86 { 87 //calculate elapsed 88 elapsed=millis()-start; 89 90 //reset start 91 start=millis(); 92 93 //calculate speed in km/h 94 speedk=(3600*circMetric)/elapsed; 95 96 //calculate speed in mph 97 speedm=(3600*circImperial)/elapsed; 98 } 99} 100 101void loop() 102{ 103 // The loop will be interrupted by the sensor each time the 104 // magnet passes near the sensor, in other words once per revolution 105 106 // Top line in the 16 char, 2 lines display - speed data 107 lcd.setCursor(0,0); 108 lcd.print(" "); 109 lcd.setCursor(0,0); 110 lcd.print(int(speedk)); 111 lcd.print(" km/h "); 112 lcd.print(int(speedm)); 113 lcd.print(" MPH "); 114 115 //bottom line the 16 char, 2 lines display - time data 116 lcd.setCursor(0,1); 117 lcd.print(" "); 118 lcd.setCursor(0,1); 119 lcd.print(int(elapsed)); 120 lcd.print(" ms/rev "); 121 122 // adjust for personal preference to minimise flicker 123 //delay(250); 124}
Downloadable files
Circuit Schematics
The whole thing clearly shown
Circuit Schematics
Comments
Only logged in users can leave comments
CesareBrizio
0 Followers
•0 Projects
Table of contents
Intro
4
0