Assignment 1 - Antiprimadonna's

on/off switch & analog sensor

On/off switch with LED light

For this antiprimadonna me and Loes made a circuit where the LED would go on when the button is pushed. The code was found here, I just changed the pins for the button and LED.

int ledPin = D2; // choose the pin for the LED
int inPin = D3;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

I also drew a schematic of this circuit. But i'm doubting this is right:

I don't think this is right because I didn't know where to put all the pins from the arduino, I feel like there is more on the board than there is on my schematic.

Ultrasonic sensor

Ive tried working with the Ultrasonic sensor, so that I could ideally change the pushbutton to the ultrasonic as a start for my working circuit. First, I just wanted to see the output of the sensor in the serial monitor just to check if the sensor was working. I did this with the following code:

#include <NewPing.h>
// ( trigger, echo, max cm)
NewPing sonar(D5, D6, 20);

void setup() {
  // put your setup code here, to run once

  Serial.begin(9600);
  delay(50);

}

void loop() {
  // put your main code here, to run repeatedly:

  Serial.print("The distance is:");
  Serial.println(sonar.ping_cm());
  delay(1000);
}

in the parameters of NewPing sonar you can how I connected the Ultrasonic to my Arduino. I've connected them with wires from the Ultrasonic straight into the Arduino pins (without breadboard, just to check). But unfortunately the distance in my Serial monitor kept staying on 0cm. So I think I accidentely took a broken ultrasonic.

First try analog Switch with LED light

I basically started here, with the crystal, without doing the first steps that were posted on Youtube, because I'm stubborn I guess. I connected the resistor with the backside of the copper tape, which is not conductive, I found out later because of Loes' whatsapp video. For some reason I thought it was possible because I vaguely remember doing something like this in week 2 with the paper circuits.

This was my first try with connecting a LED light with copper tape and the breadboard. I am not sure what I did here, the confusion began.

I decided to start over with the button and LED I have made before but then on a paper circuit, hoping I would understand it better.

First I wanted to check if the button was working so I connected it to my breadboard and sent the state of the button to the serial monitor with this code:

int pushButton = D3;
// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

And it worked:

Debugging

Then I connected the button on a paper, exactly how I connected it to a breadboard.

I checked it again with the same code, unfortunately the button didn't work anymore. This was because the pins of the button were sodered together on the copper tape. Because of this the currtent can not go around the circuit anymore. So I soldered the pins that weren't needed loose and tried it again. During the soldering I accidently broke a pin so I used another button but it worked! I could now see the right state of the button in the serial monitor. I soldered it like this:

The side that the paperclip is on togetheer with the resistor is connected GND(-), the other paperclip is connected to V3V(+).

Now I did the same thing but with the LED lights

The long side of the LED (upper one) is + and the one in the bottom right corner is GND(-). I connected the button and LED together on the breadboard and used the same code as before with the on/off switch to check if my first paper circuit was working. (And it worked, yay!)

Now that I understood the paper circuits better I wanted to take another try in a sensor paper circuit.

Second try analog switch

I used Loes' examples as a reference for the crystal sensor. I soldered everything together like she had done before with hers. I tried using the thick copper steel but unfortunately I had nothing to break and bend it with so I had to use the thin copper wire which was hard to solder on the copper tape.

Then I connected the crystal sensor like I did before with the paper LEDs and button to the breadboard to see the outputs of the sensor. I used Loes' code.

I got really weird outputs in the beginning in the serial monitor, but this was because I didnt match the baud with the serial.begin and upload speed. After Loes told me this and I changed it the sensor was working!

Then I connected 2 LEDs with the sensor to make it a bit different than Loes' example and I changed the code a bit with an if/else statement.

/*
  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe
  modified 13 March 2020 
  By Loes Bogers
  This example code is in the public domain.
  http://www.arduino.cc/en/Tutorial/AnalogInput
*/

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPinRood = D1;      // select the pin for the LED that has PWM (~) e.g pin D1 on Node MCU
int sensorValue = 0;  // variable to store the value coming from the sensor
int mappedValue = 0;  //store the mapped values
int ledPinGroen = D2;

void setup() {

  // initialize serial communication with computer:
  Serial.begin(115200);     //make sure it matches with the number in the serial port

  // declare the ledPin as an OUTPUT:
  pinMode(ledPinRood, OUTPUT);
  pinMode(sensorPin, INPUT);
  pinMode(ledPinGroen, OUTPUT);
}

void loop() {
  delay(1000);
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

//// FIND MIN & MAX RANGE FIRST, THEN COMMENT OUT
  // print values to serial to find lowest and highest values (min and max) in serial monitor
  Serial.println(sensorValue);      // met 10K voltage divider: bijv. range 100-850 zonder een fietslampje erbij (beter voor de video)
   
//PUT THE VALUES YOU FOUND (YOUR_MIN, YOUR_MAX) INTO THE LINE BELOW (line 36)
//put your min value and max value (as seen in the monitor) and map to a range 0- 255 for output
  mappedValue = map(sensorValue, 242, 1024, 0, 255); 

    // print values for debugging
    Serial.print("Old Value (no mapping) = ");
    Serial.print(sensorValue);
    Serial.print("\t");   // add a tab between the numbers
    Serial.print("New value (mapped) = ");
    Serial.println(mappedValue);

    if (sensorValue > 300) {
      digitalWrite (ledPinRood, HIGH);
      digitalWrite (ledPinGroen, LOW);
    }
      else {
        digitalWrite (ledPinRood, LOW);
        digitalWrite (ledPinGroen, HIGH);
      }
    }

If the sensor will catch light, the red led will go on and the green led is off and vice versa.

Then I tried to make the sensor in another kind of shape. Unfortunately because I understood the assignment a bit wrong and went so many ways because I didnt understood the circuit at first, I didn't had much time left for the actual creative part so I made a simple box. I would've loved to put more effort and time in this.

Box open = Red ON, green OFF Box closed = Green ON, red OFF.

Reflection

To be honest I'm not too proud of what I've made. It all took so much time because I couldn't understand how the circuits were working. I discovered that I was having trouble with understanding the assignment first, and then begin at the very first step. Instead of diving into the crystal sensor and being in a cycle of not understanding it. If I had started from the beginning I would've had more time left to make something nice. But I am happy that it is working at the end, it took a lot of time and patience.

  • From the video class Ive just learned that the crystal is not a light sensor but a pressure sensor.. :')

Last updated