0% found this document useful (0 votes)
7 views3 pages

Blynk Speedo Meter Code for ESP8266

The document contains a code snippet for an ESP8266-based speedometer project using Blynk and an LCD display. It measures RPM from a sensor, calculates speed, and sends alerts via Blynk when a certain speed threshold is exceeded. The code includes setup for WiFi connection, sensor reading, and periodic updates to the Blynk app.

Uploaded by

Dhriti Shah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Blynk Speedo Meter Code for ESP8266

The document contains a code snippet for an ESP8266-based speedometer project using Blynk and an LCD display. It measures RPM from a sensor, calculates speed, and sends alerts via Blynk when a certain speed threshold is exceeded. The code includes setup for WiFi connection, sensor reading, and periodic updates to the Blynk app.

Uploaded by

Dhriti Shah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#define BLYNK_TEMPLATE_ID "TMPL3qNiR11d9"

#define BLYNK_TEMPLATE_NAME "Speedo Meter"


#define BLYNK_AUTH_TOKEN "oF1HMLjlou9h_EHsubtBM-XjoUsN4N2B"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define motorPin D3
#define SENSOR_PIN D5 // GPIO14

LiquidCrystal_I2C lcd(0x27, 16, 2); //// SDA (Data) → D2, SCL (Clock) → D1

BlynkTimer timer;
int Map_rpm;
int flag=0;
char ssid[] = "Mmm"; //Wifi name
char pass[] = "mmmmmmmm"; //Wifi Password

volatile int pulseCount = 0;


unsigned long lastTime = 0;
float rpm = 0;
int speed = 60;
int sensorValue = 0; // value read from the pot
void UpdateBlynk() {

[Link]();
[Link](0, 0);

[Link](V0, Map_rpm);
if (Map_rpm > 100 && flag==0) {
[Link]("Gas Detected");
[Link]("high", "Speed Alert");
flag=1;
delay(1000);
}
if(Map_rpm < 100){
flag=0;
}
}
void IRAM_ATTR countPulse() {
pulseCount++;
}

void setup() {
[Link](D2,D1);
[Link]();
[Link]();
[Link](0, 0);
pinMode(motorPin, OUTPUT);
[Link](115200);
[Link](BLYNK_AUTH_TOKEN, ssid, pass);
[Link](2000L, UpdateBlynk);
pinMode(SENSOR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING);

[Link]("RPM: ");
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - lastTime >= 1000) { // 1-second interval
noInterrupts();
rpm = (pulseCount * 60.0); // Assuming one pulse per revolution
pulseCount = 0;
interrupts();
Map_rpm = map(rpm, 0, 10000, 0, 200);
[Link]();
[Link](Map_rpm);
lastTime = currentTime;
}
sensorValue = analogRead(A0);
speed = map(sensorValue, 0, 1023, 0, 300);
analogWrite(motorPin, speed); ///// range 0 to 1023
[Link](0, 0);
[Link]("Speed=");
[Link](Map_rpm);
[Link](" Km/h");
[Link]();
[Link]();
delay(50);
}

Common questions

Powered by AI

The flag variable serves as a state indicator to manage when a speed alert should be logged, specifically preventing repeated alerts during sustained high RPM conditions. This state management avoids flooding the Blynk interface with redundant events. However, potential improvements include defining multiple alert levels with a flag for each, providing more granular feedback. Additionally, implementing a debounce or time-based reset on the flag might help in more controlled alert triggering, ensuring that only genuine breaches prompt the system to log alerts, thus increasing reliability and user functionality .

The LiquidCrystal_I2C library is used in the system to control a 16x2 LCD display connected via I2C, a communication protocol that allows interfacing with the LCD using fewer wires. The system uses this library to initialize the LCD with lcd.begin and control the display of text information, such as speed measured in km/h and RPM values on the screen. It provides functions to clear the display, set cursor position, and print text, facilitating user interaction by presenting real-time feedback directly on the LCD .

The motor speed control logic efficiently maps the sensor input to motor speed using the map function, allowing for a direct translation from analog input (0-1023) to a defined speed range (0-300). This enables nuanced control over the motor's output. The system updates motor speed in real time, recalculating every loop iteration and using analogWrite for PWM signal adjustments, providing rapid, continuous feedback. However, efficiency could be improved by decoupling sensor reading frequency from main loop execution to ensure consistent performance and by optimizing the map ranges to suit specific motor demands, potentially reducing unnecessary output granularity .

To improve error handling in this system, adding checks to ensure all operations, like WiFi connections and Blynk integration, are successful can prevent issues at runtime. Implementing retries and timeout mechanisms for network-dependent functions could enhance stability. Additionally, diagnostic logs with descriptive error messages for failed operations would allow easier debugging. Ensuring that interrupt service routines execute quickly to avoid missing pulses and confirming that mapped values stay within expected ranges can prevent system misbehavior .

A speed alert is triggered in the system when the RPM value exceeds 100, which is represented by the variable Map_rpm being greater than 100, and the flag variable is 0. When these conditions are met, the system prints "Gas Detected" to the serial output and logs a "Speed Alert" event through Blynk using Blynk.logEvent. The flag is then set to 1 to prevent repeated alerts immediately following, and this state is reset to 0 when Map_rpm falls below 100, allowing new alerts for future threshold breaches .

The setup function initializes the hardware and software components of the speedometer system by configuring the wire communication on D2 and D1 for the LCD, initializing the LCD with backlight, setting pin modes for output and input (motorPin for PWM output and SENSOR_PIN for interrupt input), and enabling serial communication at 115200 baud. It also establishes a connection to the Blynk server using the defined authentication token and WiFi credentials, and sets up the timer to periodically call UpdateBlynk function. The attachInterrupt function is configured to handle sensor pulse counting, completing the setup necessary for the system's operation .

The system detects changes in motor speed using an interrupt-driven mechanism on the SENSOR_PIN (D5). Each time a pulse is detected (indicating part of a revolution of the motor), the ISR countPulse is invoked, incrementing the pulseCount variable. The RPM is calculated by multiplying the pulseCount by 60, assuming one pulse per revolution. This is done every 1000 milliseconds in the main loop, by comparing the current time to the last time the RPM was calculated, ensuring the RPM reflects the motor's speed in a real-time manner .

The Blynk Timer in the speedometer system is used to call the UpdateBlynk function at regular intervals. Specifically, in the setup function, the timer is set to trigger every 2000 milliseconds (2 seconds) using timer.setInterval(2000L, UpdateBlynk). This timely invocation allows the system to regularly update the Blynk interface with the latest RPM reading by writing Map_rpm to the virtual pin V0, and it checks for speed alerts if the RPM exceeds 100, generating a log event in the Blynk app .

The speed of the motor is controlled by reading the sensor value from an analog pin (A0), which ranges from 0 to 1023. This value is then mapped to a speed range from 0 to 300 using the map function. The resulting speed is used as a scalar in the analogWrite function to set the output on motorPin. By adjusting the PWM signal proportional to the sensor value, the analogWrite function effectively controls the motor's speed, enabling speed variation from a complete stop to the maximum defined by the system .

The attachInterrupt function is critical as it configures an external interrupt on the SENSOR_PIN (D5), enabling the speedometer system to respond to changes in the signal from the sensor rapidly and efficiently. The interrupt is set to trigger on a FALLING edge, which occurs when the signal goes from high to low, indicative of a part of the motor's rotation. This interrupt-driven approach provides precise timing and real-time reaction by updating the pulseCount variable every time the sensor detects a drop in voltage level, thereby contributing accurate real-time calculations of RPM .

You might also like