Proyecto integrado · Arduino Uno R3

Lámpara de noche con Arduino Uno

Una lámpara se enciende poco a poco cuando el pin del sensor se oscurece, con un botón en lugar del sensor de luz. Todo el proyecto, un Arduino Uno y 5 componentes más, corre aquí en tu navegador con el firmware de abajo; ábrelo en el editor para cambiar las conexiones o el código y ejecutarlo de nuevo.

¿Das clases? Asígnalo a tu grupo con un clic.

Principiante Funciona en tu navegador. Gratis y sin necesidad de cuenta.

Los componentes cuestan unos $32 en tiendas. En Mokxi son gratis.

Lámpara de noche · Arduino Uno R3live0.000 s 0.00x
Mantén presionado el botón
El circuito en sí, corriendo aquí en el simulador. Presiona lo que se pueda presionar; haz clic en cualquier otra cosa para abrirlo en el editor.

El código

El firmware exactamente como lo abre el editor. Cambia una línea ahí y presiona Ejecutar: se compila en el navegador.

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);
}

Lista de componentes

7 componentes, más los cables jumper. Cuestan unos $32 en tiendas; en Mokxi son gratis.

Cómo está conectado

6 conexiones, pin por pin, leídas del propio circuito. Cada línea es un conjunto de pines unidos entre sí, por un cable jumper o una tira de la protoboard.

  • Tierra: Arduino Uno R3 pin GND; Botón pulsador pin 2a; LED, amarillo pin C; LED, blanco pin C
  • Arduino Uno R3 pin 9; Resistencia, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 8; Resistencia, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 2; Botón pulsador pin 1a
  • Resistencia, 220 Ω (1) pin 2; LED, amarillo pin A
  • Resistencia, 220 Ω (2) pin 2; LED, blanco pin A

Cámbialo y consérvalo

Ábrelo en el editor, cambia el circuito o el código y guarda tu versión en una cuenta gratis.