Fake Automatic Sunscreen With Ultrasone Sensor

by pepijndewolff in Circuits > Arduino

100 Views, 0 Favorites, 0 Comments

Fake Automatic Sunscreen With Ultrasone Sensor

IMG_2056.JPG

Here is a step-by-step guide to replicate a smart awning that automatically opens and closes based on sunlight. This prototype has been recreated using the NodeMCU ESP8266 in conjunction with the ultrasone sensor (light sensor) and an LED strip (awning).

Supplies

IMG_2057.JPG
IMG_2058.JPG
  • NodeMCU ESP8266
  • Ultrasone sensor
  • RGB LED strip
  • USB A/C cable to micro USB

Conntect the Hardware

We'll start with connecting the hardware. Follow these wiring diagrams:

1 Connecting the Ultrasone distance sensor:

  • VCC of the sensor to the 5v pin (=Vin) FYI: the ultrasone sensor needs a 5v powersource, it doesn't work with the 3v power source.
  • GND of the sensor to one of the GND pins on NodeMCU
  • Trigger (Trig) pin of the sensor to D1 pin on NodeMCU
  • ECHO pin of the sensor to D2 pin on the NodeMCU


2 Connect the RGB LED strip:

  • VCC of the LED strip to 3v3 pin of NodeMCU
  • GND of the LED strip to the GND of NodeMCU
  • DIN of the LED strip to the D6 pin on NodeMCU

Downloads

Install the Required Libraries

Screenshot 2023-10-18 at 16.51.24.png
Screenshot 2023-10-18 at 16.50.29.png

Open the Arduino IDE and install the following libraries if you haven't already:

  • Adafruit NeoPixel: Go to "Sketch" > "Include Library" > "Manage Libraries" and search for "Adafruit NeoPixel." Install the library.


Insert the Code

Copy and paste the code as shown below:

#include <Adafruit_NeoPixel.h>

#define PIN_LED_STRIP D6 // De digitale pin waaraan de LED-strip is aangesloten
#define NUM_LEDS 9 // Het aantal LED's op de strip


#define TRIGGER_PIN D1 // De trigger pin van de ultrasone afstandssensor
#define ECHO_PIN D2 // De echo pin van de ultrasone afstandssensor


Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN_LED_STRIP, NEO_GRB + NEO_KHZ800);


void setup() {
strip.begin();
strip.show(); // Zet alle LED's uit bij het opstarten


pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
}


void loop() {
// Stuur ultrasoon signaal om de afstand te meten
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);


// Bereken de afstand in centimeters
float distance = (duration / 2) / 29.1;


if (distance < 20) { // Pas deze drempelwaarde aan op basis van je eigen configuratie
// Weinig licht (object dichtbij), zonnescherm moet open zijn
fadeOutLEDStrip();
} else {
// Veel licht (geen object in de buurt), zonnescherm moet dicht zijn
fadeInLEDStrip();
}
}


void fadeOutLEDStrip() {
for (int i = NUM_LEDS - 1; i >= 0; i--) {
strip.setPixelColor(i, strip.Color(0, 0, 0)); // Zet de LED uit
strip.show();
delay(200); // Vertraging om langzaam uit te faden
}
}


void fadeInLEDStrip() {
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(255, 255, 255)); // Zet de LED aan (wit)
strip.show();
delay(200); // Vertraging om langzaam in te faden
}
}


Upload the code!


Possible errors:


1 LED's not working

Check if the amount of LED's in strip is stated correctly. If not change to correct number of LED's in the strip

#define NUM_LEDS 9          // Het aantal LED's op de strip


2 Nothing is happening

If you have connected everything correctly according to the instructions and uploaded the code to the NodeMCU, but nothing happens, there are several steps you can take to troubleshoot the issue:

  • Check your connections: Make sure that all wires and connections are correctly and securely attached. Verify that there are no loose or poorly connected wires.


  • Power supply: Ensure that the power supply voltage (VCC) and ground (GND) are correctly connected and that the voltage is sufficient for both the ultrasonic distance sensor and the LED strip.


  • Check the ultrasone distance sensor: Ensure that the ultrasonic distance sensor is functioning correctly. Sometimes, these sensors can be defective. Test the sensor separately to ensure that it accurately measures distances.

To test the Ultrasone sensor seperatly open a new sketch and add this code:

			#define TRIGGER_PIN D1   // De trigger pin van de ultrasone afstandssensor
#define ECHO_PIN D2      // De echo pin van de ultrasone afstandssensor


void setup() {
  Serial.begin(9600);
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}


void loop() {
  long duration, distance;


  // Stuur ultrasoon signaal om de afstand te meten
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);


  duration = pulseIn(ECHO_PIN, HIGH);
  
  // Bereken de afstand in centimeters
  distance = (duration / 2) / 29.1;


  Serial.print("Afstand: ");
  Serial.print(distance);
  Serial.println(" cm");


  delay(1000);  // Wacht 1 seconde voordat de volgende meting wordt uitgevoerd
}


Make sure that your Arduino IDE is open and that your NodeMCU is connected to your computer via USB.

Go to the "Tools" menu at the top of the Arduino window.

In the "Tools" menu, select "Serial Monitor." This will open a new window.

In the serial monitor window, you can set the baud rate by clicking on the value in the lower right corner (the default is usually 9600 baud). Ensure that the baud rate matches the baud rate you have set in your Arduino code (e.g., Serial.begin(9600);).

If it keeps displaying: 'afstand = 0m' then your Ultrasone sensor is probably defect...


  • Timing and threshold values: Check if the timing (delays) and threshold values in the code are suitable for your specific situation. Adjust the threshold values based on the conditions in which you are using the system.


  • Check the LED strip: Make sure that the LED strip itself is working. You can test the LED strip separately by writing a simple program for it without the ultrasonic distance sensor.


  • Code errors: Check if there are any errors or warnings when you compile and upload the code. Ensure that the Adafruit NeoPixel library is correctly installed.


  • Power consumption: Verify that the power source is providing sufficient current for both the NodeMCU and the LED strip. LED strips can consume relatively high current, so ensure that the power supply can handle it.


Testing and Finetuning

Place an object near the ultrasonic distance sensor to test the operation of the awning (LED strip). Adjust the threshold values and delays in the code until the desired behavior is achieved.


Adjusting delay:

void fadeOutLEDStrip() {
for (int i = NUM_LEDS - 1; i >= 0; i--) {
strip.setPixelColor(i, strip.Color(0, 0, 0)); // Zet de LED uit
strip.show();
delay(200); // Vertraging om langzaam uit te faden
}
}

You can change the delay to the desired value


DONE!