Skip to content

7. Input & Output device

In this week we learned how to connect the input (photoresistor) and output (addressable rgb led) devices through microcontroller (Adafruit Feather).

Microcontroller

Adressable RGB LED

Photoresistor

At the beggening I learned how to change the color of the addressable rgb led

Then I learnd how to conect the sensor and see the reading of input

then I write a code and connect the input and output devices

// Include the necessary library
#include <Adafruit_NeoPixel.h>

// Define the pin for the LED strip
#define LED_PIN 6

// Define the number of LEDs in the strip
#define NUM_LEDS 60

// Define the pin for the light sensor
#define LIGHT_SENSOR_PIN A0

// Define the brightness levels
#define BRIGHT_LEVEL 255
#define MID_BRIGHT_LEVEL 100
#define DARK_LEVEL 0

// Create a NeoPixel object
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  // Initialize the NeoPixel strip
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Read the value from the light sensor
  int lightSensorValue = analogRead(LIGHT_SENSOR_PIN);
Serial.println(lightSensorValue);
  // Adjust the brightness based on the light sensor value
  if (lightSensorValue > 800) {
    // Bright outside, turn off the LEDs
    adjustBrightness(DARK_LEVEL);
  } else if (lightSensorValue > 500) {
    // Somewhat dim outside, set to mid brightness
    adjustBrightness(MID_BRIGHT_LEVEL);
  } else {
    // Dark outside, set to high brightness
    adjustBrightness(BRIGHT_LEVEL);
  }

  // Delay for a short time
  delay(100);
}

void adjustBrightness(uint8_t brightness) {
  // Set the brightness of all LEDs
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color( brightness,  brightness, brightness));
  }
  strip.setBrightness(brightness);
  strip.show();
}


Last update: August 5, 2024