Hazır proje · Arduino Uno R3

Arduino Uno'da gece lambası

Sensör pini karardığında bir lamba yavaşça yanar; ışık sensörünün yerini bir buton tutar. Tüm yapım, bir Arduino Uno ve 5 parça daha, aşağıdaki firmware üzerinde tarayıcında çalışır; kablolamayı ya da kodu değiştirmek ve yeniden çalıştırmak için düzenleyicide aç.

Öğretiyor musun? Tek tıkla sınıfına ata.

Başlangıç About 15 minutes to build and run. Tarayıcında çalışır. Ücretsiz, hesap gerekmez.

Parçalar yaklaşık $32'a satılıyor. Mokxi'de ücretsiz.

Gece lambası · Arduino Uno R3canlı0.000 s 0.00x
Butona basılı tut
Devrenin kendisi, burada simülatörde çalışıyor. Basılabilecek şeylere bas; düzenleyicide açmak için başka bir şeye tıkla.

What you'll learn

  • Reading an input with an internal pull-up resistor
  • Fading an LED with analogWrite on a PWM pin
  • Moving toward a target a little at a time instead of jumping
  • Why a button can stand in for a sensor while you test
  • Difficulty: Başlangıç
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 7

Nasıl çalışır

A pushbutton on pin 2 stands in for a light sensor. It uses the internal pull-up, so the pin reads HIGH when nothing is pressed (light) and LOW when you hold the button (dark).

When it is dark, the sketch wants the lamp at full brightness, 255. When it is light, it wants 0. It does not jump. Every 20 ms it moves the lamp level toward that target by 8, so the fade takes about six tenths of a second.

The lamp is the yellow LED on pin 9, which can do PWM. The white LED on pin 8 is an indicator that shows what the sensor reads right now, even while the lamp is still fading.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Parça ve pinConnects to
Pushbutton pin 2aArduino Uno R3 pin GND
LED, yellow pin CArduino Uno R3 pin GND
LED, white pin CArduino Uno R3 pin GND
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 9
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 8
Pushbutton pin 1aArduino Uno R3 pin 2
LED, yellow pin AResistor, 220 Ω (1) pin 2
LED, white pin AResistor, 220 Ω (2) pin 2

Kod

Düzenleyicinin açtığı şekliyle firmware. Orada bir satırı değiştir ve Çalıştır'a bas: tarayıcıda derlenir.

sketch.ino · Arduino Uno R3
// Night light: the lamp fades up when it goes dark and down when it is light.
//
//   pin 2  the light sensor, to GND, internal pull-up
//   pin 9  the lamp, through 220 ohm to GND
//   pin 8  the indicator, through 220 ohm to GND
//
// The sensor here is a pushbutton, standing in for the light-dependent
// resistor a real night light has. Mokxi has an LDR part, but a button is
// easier to hold, and the two behave the same way as far as the sketch is
// concerned: the pin is high while there is light and low while there is not.
// Hold the button and the room is dark.
//
// The lamp is on pin 9, which is one of the ATmega328P's PWM outputs, so
// analogWrite can give it any brightness from 0 to 255. It walks there rather
// than jumping: a night light that snaps on is a torch, and the ramp is what
// makes it a night light. Eight counts every twenty milliseconds is about six
// tenths of a second end to end.
//
// The indicator on pin 8 is a plain digital output and says what the sensor is
// reading right now, so you can see the input and the output disagree while
// the lamp is still on its way.

const int SENSOR = 2;
const int LAMP = 9;
const int INDICATOR = 8;

const int STEP = 8;
const int FULL = 255;

int level = 0;

static bool dark() { return digitalRead(SENSOR) == LOW; }

void setup() {
  pinMode(SENSOR, INPUT_PULLUP);
  pinMode(LAMP, OUTPUT);
  pinMode(INDICATOR, OUTPUT);
  analogWrite(LAMP, 0);
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: night light");
  Serial.println("the button stands in for a light sensor: hold it for dark");
}

void loop() {
  bool night = dark();
  digitalWrite(INDICATOR, night ? HIGH : LOW);

  int want = night ? FULL : 0;
  if (level < want) {
    level += STEP;
    if (level > want) {
      level = want;
    }
  } else if (level > want) {
    level -= STEP;
    if (level < want) {
      level = want;
    }
  }
  analogWrite(LAMP, level);

  static int said = -1;
  if (level == want && level != said) {
    said = level;
    Serial.print(night ? "dark: lamp at " : "light: lamp at ");
    Serial.println(level);
  }

  delay(20);
}

Code walkthrough

The key functions of the sketch, and what each one does.

dark sketch.ino

Returns true when pin 2 reads LOW. Because of the pull-up, LOW means the button is held, which stands for a dark room. Wrapping the test in a function keeps the rest of the code easy to read.

static bool dark() { return digitalRead(SENSOR) == LOW; }

setup sketch.ino

Sets the sensor pin as INPUT_PULLUP and the lamp and indicator pins as outputs. It starts the lamp at 0 with analogWrite and prints two lines to the serial monitor.

void setup() {
  pinMode(SENSOR, INPUT_PULLUP);
  pinMode(LAMP, OUTPUT);
  pinMode(INDICATOR, OUTPUT);
  analogWrite(LAMP, 0);
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: night light");
  Serial.println("the button stands in for a light sensor: hold it for dark");
}

loop sketch.ino

Sets the indicator to match dark, then picks a target of FULL or 0. It moves level toward the target by STEP, without overshooting, and writes it to the lamp. Once the lamp arrives, it prints the level once. Then it waits 20 ms.

void loop() {
  bool night = dark();
  digitalWrite(INDICATOR, night ? HIGH : LOW);

  int want = night ? FULL : 0;
  if (level < want) {
    level += STEP;
    if (level > want) {
      level = want;
    }
  } else if (level > want) {
    level -= STEP;
    if (level < want) {
      level = want;
    }
  }
  analogWrite(LAMP, level);

  static int said = -1;
  if (level == want && level != said) {
    said = level;
    Serial.print(night ? "dark: lamp at " : "light: lamp at ");
    Serial.println(level);
  }

  delay(20);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Holding the button does nothing. This project
    Wire pin 2 to one side of the button and the other side to GND. The pull-up is inside the Uno.
  • The yellow lamp does not fade. This project
    Check that its resistor is on pin 9, which is a PWM pin. A pin without PWM would only jump on or off.
  • An LED stays dark. This project
    Check its direction: the anode goes toward the resistor, and the cathode goes to GND.
  • The input reads random HIGH and LOW when the button is not pressed. Buton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Buton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Direnç
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Direnç
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.

Değiştir ve sakla

Düzenleyicide aç, devreyi ya da kodu değiştir ve sürümünü ücretsiz bir hesapta sakla.