Skip to content

8. Input & Output device

This week’s tasks:

a) Measure something: Add a sensor to a microcontroller board and read it.

b) Add an output device to a microcontroller board and program it to do something.

We got introduced to various inputs like temperature, distance, humidity and light. These inputs when given a power source such as a battery or a usb work the way you encode it oetc. and input devices pins connected to microcontroller.

Heading into the week of our final project, we finalized what elements are we going to need for it. We will be using lights, rotary motor and motion sensor

Light

We wanted our led strips to have a cycle of our chosen colors

#include <Adafruit_NeoPixel.h>

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(17, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit... if you must, connect GND first.

void setup() {
  strip.begin();
  strip.show(); // initialize all pixels to "off"
}

void loop() {

  brighten();
  darken();
  brighten1();
  darken1();
  brighten2();
  darken2();
  brighten3();
  darken3();
  brighten4();
  darken4();


}

// 0 to 255 blue
void brighten() {
  uint16_t i, j;

  for (j = 10; j < 255; j++) {
    strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, 255);
    }
    strip.show();
    delay(5);
  }
  //delay(1500);
}


// 0 to 255 PURPLE
void brighten1() {
  uint16_t i, j;

  for (j = 10; j < 200; j++) {
    strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 160, 32, 240);
    }
    strip.show();
    delay(5);
  }
  //delay(1500);
}

// 255 to 0 blue
void darken() {
 // Serial.begin(9600);
  uint16_t i, j;

  for (j = 200; j > 10; j--) {
        strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, 255);
    }
    strip.show();
    delay(5);
   // Serial.println(j);
  }
  delay(1500);
}

// 255 to 0 PURPLE
void darken1() {
 // Serial.begin(9600);
  uint16_t i, j;

  for (j = 200; j > 10; j--) {
        strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 160, 32, 240);
    }
    strip.show();
    delay(5);
   // Serial.println(j);
  }
  delay(1500);
}
// 0 to 255 PINK
void brighten2() {
  uint16_t i, j;

  for (j = 10; j < 200; j++) {
    strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 255, 0, 128);
    }
    strip.show();
    delay(5);
  }
  //delay(1500);
}

// 255 to 0 PINK
void darken2() {
 // Serial.begin(9600);
  uint16_t i, j;

  for (j = 200; j > 10; j--) {
        strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 255, 0, 128);
    }
    strip.show();
    delay(5);
   // Serial.println(j);
  }
  delay(1500);
}
// 0 to 255 RED
void brighten3() {
  uint16_t i, j;

  for (j = 10; j < 200; j++) {
    strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 255, 0, 0);
    }
    strip.show();
    delay(5);
  }
  //delay(1500);
}

// 255 to 0 RED
void darken3() {
 // Serial.begin(9600);
  uint16_t i, j;

  for (j = 200; j > 10; j--) {
        strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 255, 0, 0);
    }
    strip.show();
    delay(5);
   // Serial.println(j);
  }
  delay(1500);
}
// 0 to 255 ORANGE
void brighten4() {
  uint16_t i, j;

  for (j = 10; j < 200; j++) {
    strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 255, 165, 0);
    }
    strip.show();
    delay(5);
  }
  //delay(1500);
}

// 255 to 0 ORANGE
void darken4() {
 // Serial.begin(9600);
  uint16_t i, j;

  for (j = 200; j > 10; j--) {
        strip.setBrightness(j);

    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 255, 165, 0);
    }
    strip.show();
    delay(5);
   // Serial.println(j);
  }
  delay(1500);
}

Rotary Motor

Our project would also need a rotation mechanism of some kind. We use this stepper motor:

//Includes the Arduino Stepper Library
#include <Stepper.h>

// Defines the number of steps per rotation
const int stepsPerRevolution = 4056;

// Creates an instance of stepper class
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper = Stepper(stepsPerRevolution, 6, 10, 5, 9);

void setup() {
    // Nothing to do (Stepper Library sets pins as outputs)
}

void loop() {
    // Rotate CW slowly at 5 RPM
    myStepper.setSpeed(1);
    myStepper.step(stepsPerRevolution);
//  delay(1000);
}

This code didn’t work with our Adafruit Sense microcontroller but it’s worth trying it with other microcontrollers.

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}

Motion Sensor

To activate our project, we opted for motion sensor using the Adafruit Feather Sense.

/***************************************************************************
  This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor

  This sketch puts the sensor in gesture mode and decodes gestures.
  To use this, first put your hand close to the sensor to enable gesture mode.
  Then move your hand about 6" from the sensor in the up -> down, down -> up, 
  left -> right, or right -> left direction.

  Designed specifically to work with the Adafruit APDS9960 breakout
  ----> http://www.adafruit.com/products/3595

  These sensors use I2C to communicate. The device's I2C address is 0x39

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Dean Miller for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include "Adafruit_APDS9960.h"
Adafruit_APDS9960 apds;

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(115200);

  if(!apds.begin()){
    Serial.println("failed to initialize device! Please check your wiring.");
  }
  else Serial.println("Device initialized!");

  //gesture mode will be entered once proximity mode senses something close
  apds.enableProximity(true);
  apds.enableGesture(true);
}

// the loop function runs over and over again forever
void loop() {
  //read a gesture from the device
    uint8_t gesture = apds.readGesture();
    if(gesture == APDS9960_DOWN) Serial.println("v");
    if(gesture == APDS9960_UP) Serial.println("^");
    if(gesture == APDS9960_LEFT) Serial.println("<");
    if(gesture == APDS9960_RIGHT) Serial.println(">");
}

Last update: July 20, 2024