Distancing Hat
#define
FASTLED_ESP8266_RAW_PIN_ORDER
#include <FastLED.h>
// define pins numbers
const int trigPin = 4; //D2
const int echoPin = 5; //D1
const int alertPin = 14; //D5
const int ledPin = 12; //D6
// Define the approach distances centimetres.
const int alertDistance = 200; // Alarm sounds red led
const int yellowDistance = 280; // Yellow led
// led colours
int red = 0xff0000;
int yellow = 0xff5b00;
int green = 0x00ff00;
// About the leds
#define NUM_LEDS 3
#define LED_PIN ledPin
#define LED_TYPE PL9823
// Tell the library about the number of leds
CRGB led[NUM_LEDS];
// define distance variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Set the trigPin as an
Output
pinMode(echoPin, INPUT); // Set the echoPin
as an Input
pinMode(alertPin, OUTPUT); // Set the alert to output.
Serial.begin(9600); // Start the serial communication
// Initialize the fast led library
FastLED.addLeds<LED_TYPE, LED_PIN>(led, NUM_LEDS);
delay(1000);
// Set the initial led colours
led[0] = 0x000000; // pilot light and 5v drop
through diode
led[1] = led[2] = green;
}
void loop() {
// Clear the trigger pin and alert pin
digitalWrite(trigPin, LOW);
digitalWrite(alertPin, LOW);
delayMicroseconds(2);
// Set the trigger pin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time
in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance - speed of sound is 343 mps
distance = duration * 0.0343 / 2;
// Set the leds to green
led[1] = led[2] = green;
if (distance < yellowDistance) {
led[1] = led[2] = yellow;
}
if (distance < alertDistance) {
digitalWrite(alertPin, HIGH);
led[1] = led[2] = red;
}
// Display the set led colour.
FastLED.show();
delay(200);
// Display the distance on the Serial Monitor
Serial.print("Distance cm: ");
Serial.println(distance);
}
-
Circuits
- There are 3 circuits:
- The ultrasonic sensor
- Connect Vcc (+5) and ground and trigger and echo to to the
ESP. The sensor does not work powered with 3.3V
- The trigger and echo are attached to pins 4 and 5
respectively. So far the 5V in on the digital pin has
not broken the ESP.
- The beeper
- Simply pin 14 to Vcc on the beeper and ground.
- The Leds.
- The leds want 5 volts, but the output of the esp is only
3.3 volts and the leds expect a logic high to be a minimum
of .7 * Vcc. = 3.5V. So if we power the leds
with 5 Volts but send it a signal of 3.3V from the ESP it
may not see the logic high in the control signal. My
experience is sometimes it works and sometimes it doesn't.
- A solution. Power the first led through a
diode. So the first led sees 5 - 0.7V = 4.3V.
4.3 *.7 = 3.01. So 3.3V will be seen as a logic
high. The rest of the leds (2) are powered by
5V. The first led is essentially sacrificial.
- Connections to the leds.
- Vcc 3.3 and 5V depending on which one.
- Ground.
- Data In - the first led is connected to pin 12.
- Data Out of led 1 to Data In of led 2, Data Out of led 2
to Data In of led 3. Led 3 Data Out is not
connected.