SKU: 17916
KY-027 Magic Light Cup Sensor 4pin Module diy Starter Kit KY027
KY-027 Magic Light Cup Sensor Popis Vlastnosti Výrobca Model KY-027 Použitie Detekcia okolitého svetla Napájanie 3.3V – 5V…
Popis
KY-027 Magic Light Cup Sensor
Popis Vlastnosti
Výrobca
Model KY-027
Použitie Detekcia okolitého svetla
Napájanie 3.3V – 5V DC
Pinovanie AO, GND, +, S
Príklady použitia:
Príkladový kód pre Arduino:
// Include the library
#include
// Define the sensor pin
int sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int lightLevel = analogRead(sensorPin);
Serial.print("Light Level: ");
Serial.println(lightLevel);
delay(1000);
}Príkladový kód pre ESPHome:
sensor:
- platform: adc
pin: A0
name: "Light Sensor"
update_interval: 60sNodeMCU & KY-027 Magic light cup module
The KY-027 is part of the 37 sensor set.(just the KY-027 will cost you less than a Euro online). In short it’s 2 components combined, a mercury tilt switch and a led. You can actually use this input sensor with just 2 batteries:
Connect G to the – side of the battery pack
Connect + to the + side of the battery pack
Connect S (switch) to the L (LED)
The magic is, when you tilt the switch, the led will turn on and off.
Of course it is also possible to connect it to the NodeMCU.
Connect G to a GND port of your NodeMCU
Connect + to a 3.3v port of your NodeMCU
Connect S to a digital port on your NodeMCU (in my example port D3)
Connect L to a digital port on your NodeMCU (in my example port D5)
Let’s start with the led:
gpio.mode(5,gpio.OUTPUT) gpio.write(5,gpio.HIGH) -- this should turn on the led gpio.write(5,gpio.LOW) -- this should turn off the led
The mercury tilt is essentially a switch:
gpio.mode(3,gpio.INPUT) print(gpio.read(3)) -- tilt the switch and re-execute the above statement
A 1 means the switch is not making contact, a 0 means there is a contact (the tit.
Now to combine this with a trigger, turn the led off if the switch is upright (and making contact) and turn the led on if there is no contact:
gpio.trig(3,"both",function(level) gpio.write(5,level) end)
___________
KY-027 magic light cup Tilt Switch Module with Arduino
simply iot sensorsOctober 16, 2021
How to use magic light cup KY-027 with Arduino and how the magic light cup module work are some questions that may arise in your mind while programming an Arduino.
Hello guys! Welcome back to my blog today. We are going to see about the magic light cup (KY-027) sensor.
What is Magic Light Cup Tilt Switch??
The magic light cup module is a combination of an LED module and a Tilt switch module. The Mercury ball inside the Tilt switch causes the LED to TURN OFF and TURN ON. The tilt switch consists of a mercury ball. it is sometimes called a mercury opening module
A tilt switch is a switch that uses mercury as a conductive material to make electrical contact between two leads. A tilt switch consists of a tube inside which a mercury ball is used to make a contact between the lead terminals.
How does Magic Light Cup Tilt Switch Works??
When the tilt switch is placed at the vertical position, the mercury ball falls to the bottom of the tube, as is a good conductor of electricity it generates electrical contact between the lead terminal thus acting as a Closed circuit.
When you change the position of the switch in the horizontal direction the mercury ball which was at the bottom rolls towards the side direction and hence there is no contact between the lead terminal, therefore, it acts as an Open-circuit.
The circuitry of the module is such that when the mercury ball is at the bottom of the glass the voltage at the signal pin will be LOW and the led module will be turned OFF. When we roll the mercury ball the voltage at the signal pin will be HIGH and the led module will be turned ON.
Parts Required
KY-027 (magic light cup Tilt Switch Module Arduino)
Jumper wires
LED
Breadboard
Schematic Diagram
Connect the ground pin of the tilt switch to the ground pin of Arduino, the VCC pin to 5v, and the Signal pin to 8. Also, connect the positive lead of LED to Arduino pin 10.
Code
int ledmodule=8; // Set led pin at Arduino pin 8
int tiltmodule=10; // Set sensor pin at Arduino pin 10
int sensorvalue; // initialize variable to store sensor data
void setup()
{
pinMode(ledmodule,OUTPUT); // LED pin set to output
pinMode(tiltmodule,INPUT); // tilt sensor pin set to input
Serial.begin(115200); // set baud rate for Serial communication
}
void loop()
{sensorvalue = digitalRead(tiltmodule); // read values from sensor pin and store in variable
if (sensorvalue==HIGH) // checking conditions
{
digitalWrite(ledmodule,HIGH); // turn LED on
Serial.println(“Rolling”); // Print message
}
else{
digitalWrite(ledmodule,LOW); // turn LED off
Serial.println(“Stable”); // Print message
}
delay(20); // delay
}
First, initialize pins of sensors & LED and variables. We have declared a variable name sensor value to read values from sensors. In the void setup, we configure pins as INPUT or OUTPUT. We want to read values from the sensor pin so we will declare it as INPUT. Also, we configure the baud rate to 115200.
In a void loop, we continuously read values from the tilt switch and store values in variable name sensor value and we check here condition if it is HIGH or not. Normally by default sensor sends a low signal when the sensor is at a stationary position means the mercury ball is at the bottom and connects two leads to make a close circuit. And HIGH means mercury ball is moved away from two leads making the connection as open circuit, the sensor sends a HIGH signal to our Arduino which we printed as “Rolling”. Here we have written code to turn ON led when there is a movement of the mercury ball.
When the mercury ball is at the bottom of the tube connecting two leads then the sensor sends a LOW signal to Arduino which is printed as “Stable“ and the LED module connected to pin 10 will be turned OFF. We have introduced a delay of 20 milliseconds. The sensor output may differ depending upon the pull-up and pull-down onboard resistor.
Output
You can see output on the Serial monitor. When the tilt switch is at the stable position, the mercury ball is at the base then the output will be “Stable” and when we make a move to the sensor such that the mercury ball goes away from the base of the tube, then sensor output will be “Rolling”.
Conclusion –
Today we learn about what is a magic light cup, how to use a magic light cup with Arduino & working principle of the magic light cup.
“I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog.”
_________________
KY-027 Magic Light Cup Sensor
Popis
Senzor KY-027 je jednoduchý senzor svetla, ktorý sa používa na detekciu okolitého osvetlenia.
Vlastnosti
Detekcia svetla
Integrovaný potenciometer na nastavenie citlivosti
Jednoduché pripojenie cez 4 pinový konektor
Špecifikácie
Parameter Hodnota
Napájanie 3.3V – 5V
Výstupný signál Digitálny
Rozmery 19mm x 15mm
Príklady použitia
Automatizácia osvetlenia v miestnostiach
Kontrola osvetlenia v rôznych scenároch IoT projektov
Príkladový kód pre Arduino
// Názov: Príklad kódu pre KY-027 Magic Light Cup Sensor s Arduinom // Autor: Tvoje meno // Webová stránka: www.tvojastranka.com
int sensorPin = A0; // Pripojenie senzora na analógový vstup A0
int sensorValue; // Premenná pre uloženie hodnoty z senzora
void setup() {
Serial.begin(9600); // Inicializácia sériovej komunikácie
}
void loop() {sensorValue = analogRead(sensorPin); // Čítanie hodnoty zo senzora Serial.println(sensorValue); // Vypísanie hodnoty na sériový port delay(1000); // Čakanie 1 sekundu }
Príkladový kód pre ESPHome
V súčasnosti nie je k dispozícii príkladový kód pre ESPHome, ale senzor KY-027 môže byť integrovaný pomocou dostupných knižníc pre senzory svetla.
KY-027 4kolíkový modul snímača magického svetla
Princíp stmievania spočíva v použití princípu zmeny jasu dvoch modulov pre pwm.
Ortuťové spínače poskytujú digitálny signál, ktorý spúšťa PWM, prostredníctvom návrhu programu,
Svetlo vidíme ako dve šálky naplnené efektom miešania sa tam a späť.
testovací kód:
int ledPinA = 9;
int switchPinA = 8;
int switchStateA = 0;
int ledPinB = 6;
int switchPinB = 7;
int switchStateB = 0;
int brightness = 0;
void setup()
{
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
pinMode(switchPinA, INPUT);
pinMode(switchPinB, INPUT);
}
void loop()
{
switchStateA = digitalRead(switchPinA);
if (switchStateA == HIGH && brightness != 255)
{
brightness ++;
}
switchStateB = digitalRead(switchPinB);
if (switchStateB == HIGH && brightness != 0)
{
brightness --;
}
analogWrite(ledPinA, brightness); // A slow fade out
analogWrite(ledPinB, 255 - brightness); // B slow bright up
delay(20);
}KY-027 (A)
G GND
+ 5v
S 8
L 9
KY-027 (B)
G GND
+ 5v
S 7
L 6
V tomto náčrte Arduina použijeme oba moduly KY-027 na vytvorenie efektu magického svetelného pohára. Ortuťové spínače v každom module poskytujú digitálny signál, ktorý sa používa na reguláciu jasu LED diód pomocou PWM. Naklonením modulov sa zníži jas na jednom module, zatiaľ čo na druhom sa zvýši, čím sa vytvorí ilúzia svetla magicky prechádzajúceho z jedného modulu na druhý.