9. Input Devices

This week we focused on Input Devices. As a group we discussed and probed an input device to read the analog levels and the digital signals. Below you will find the group results.

Digital Input

To read a digital input, an HC-SR04 ultrasonic sensor was hooked up to Christopher’s modular ATtiny84 circuit board. The board reads data from the sensor and displays it on a serial monitor. A link has been provided to Christopher’s Input device page for more detailed information:

Here’s a short video of it in action:

Analog Input

For the analog input readings we used Alecia’s final board. This board is already set up with a phototransistor that controls the output of an RGB LED.

In order to use this board with the phototransistor we needed to connect the board to a laptop using an FTDI cable. This was linked to the ESP 32 that was used for programming.

Arduino Software was used to upload the code created by Alecia Gorski that follows.

// Written by Alecia Gorski

//uses a phototransistor reading and transmits to the esp32, this then turns the led on or off
//based on the readings received from the phototransistor 

//Fab Academy 2020 (July)


int photoTran = 27;
int reading = 0;
int LEDpin3V3 = 2;
int LEDpinR = 26;
int LEDpinG = 25;
int LEDpinB = 32;

void setup(){
pinMode(photoTran,INPUT);
Serial.begin(9600);
pinMode(2, OUTPUT); //sets the digital pin 2 as output
pinMode(26, OUTPUT); //sets the digital pin 26 as output
pinMode(25, OUTPUT); //sets the digital pin 25 as output
pinMode(32, OUTPUT); //sets the digital pin 32 as output
}

void loop() {
reading = analogRead(photoTran);
Serial.println(reading);
delay(100);

if (reading > 3800) {
  digitalWrite(LEDpin3V3, LOW);
  digitalWrite(LEDpinR, LOW);
  digitalWrite(LEDpinG, LOW);
  digitalWrite(LEDpinB, LOW);
  //LED should be on during the night
}

else if (reading < 3800) {
  digitalWrite(LEDpin3V3, HIGH);
  digitalWrite(LEDpinR, HIGH);
  digitalWrite(LEDpinG, HIGH);
  digitalWrite(LEDpinB, HIGH);
  //LED should be off during the day 
}
 }

Seen in the video below, the phototransistor can be read using the Arduino serial port. As light is being let into the phototransistor it reads as low as 48 and the RGB LED is turned off. When the phototransistor is no longer receiving light it will record readings as high as 4095. This then triggers the RGB LED to light. As the code above shows, any reading from the phototransistor above 3800 triggers the RGB to be set as LOW and it turns off. Any reading below 3800 the RGB LED is set to HIGH and it is turned on.

This may seem a bit confusing at fist. The code sounds like it should do the opposite of what the videos reaction is. However, I believe this has something to do with the combination of analogWrite and digitalWrite within the code. When uploaded with the HIGH and LOW values switched there was no reaction being recorded.