Week 6: Electronics & Open Design

Reading values using an LDR sensor

In order to start working on my own sensor, I first hooked up the LDR sensor on my breadboard. With this setup, and the code that Loes provided, I would be able to read values that the LDR sensor picks up. The LDR sensor is a light sensor. The values it gives when it is not covered up (e.g. not receiving any light) are different from when it's not covered up. These values can then be implemented in the code so that the LED will light up gradually, depending on the amount of light the LDR sensor picks up.

I set up the LDR sensor on my breadboard and Arduino NodeMCU, as shown below:

I was only going to read the values for now. Looking at the serial monitor and covering up the LDR sensor, I got the following values:

When reading these values, I noted down the minimum and maximum value. In my case this was 189 and 604. As you can see, the values are different in the video above. This is because it was recorded on a different time of day, causing it to be darker, and thus values to be lower. The values are then to be put in the part of the code that is now still a paragraph of comments, as explained by Loes in the tutorial video (link to video).

When I first tried to upload the finished code to the Arduino, I got a strange error saying: "mappedValue does not name a type". I got very confused, and even got some very dark flashbacks to my times as a Computer Science student...debugging is very frustrating. Luckily, thanks to the internet, I was able to find out I forgot to remove an opening bracket, causing the last bit of code to fall out of the void loop. Fixing this caused the code to be able to successfully upload to my Arduino.

I was however very sad to see my LED not lighting up at all. In the process of trying to check every single wire and connection, I got so lost I was thinking about giving up. Luckily I was able to calm myself and carefully check the entire LED connection on the breadboard. I noticed that the resistor wasn't properly connected (or lined up) with the LED. Also the LED to ground connection wasn't correct. Fixing these two issues caused the light to finally work, as shown below.

Here is the code I used:

/*
  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 ledPin = 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

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(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
}

void loop() {
  // 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 189-604 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, 189, 604, 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);

    // turn the ledPin on using the mappedvalue from the sensorpin
    analogWrite(ledPin, mappedValue);
}

Making a velostat sensor

With the knowledge and code I learned using the LDR sensor (and Loes' tutorials), it was now time to make my own sensor. I wanted to use velostat to make this sensor, becasue I like the pressure-sensitive properties it has. Before I wanted to make a sensor, I wanted to see if I could get any values using velostat. I hooked up a simple sensor, using nothing but copper tape and a piece of velostat.

Opening the serial monitor showed me that, when pressed with maximum force, the value it will give is 1024. When left in a resting state, the numbers jump around a bit, but never above 100. So all I had to do was adjust the values in the code to 100 and 1024, and I should work properly. Unfortunately, this wouldn't make for a gradual increase in light intensity. Instead, the slightest touch would cause the light to light up. That's why I chose to increase the minimum value to 500. The same problem occurred. Then I picked 950 as a minimum value, and it worked! Now I had to transform my velostat sandwich into a proper sensor. I figured out that my simple sensor was the blueprint for a more complex one: it had to be to pieces of copper touching the same piece of velostat, but not each other. That would mean the LED would always light up.

It was time to design my own sensor. Below I have grouped up all five iterations, which I will explain further.

So I started with something simple: a 2D version of my velostat sandwich: a piece of velostat connecting two pieces of copper tape. I first thought a thin piece of velostat would be good because it would use up less material. I quickly concluded however that this would make the pressing area very small and thus harder to use. That's why, for the second iteration, I increased the height size of the piece of velostat, and for the third iteration even the width of the piece of velostat. The third iteration wasn't of much use however, because the amount of copper tape covered is not relevant. The second iteration proved more useful, but this looked an awful lot like Loes' design (sorry Loes!). I needed to do something different with it. I wanted to turn into a 3D sensor (meaning that it could fold). The fourth iterations shows a piece of velostat in the middle with two pieces of copper tape that can fold to the middle. Although cool in theory, with this wired the two wires that connect to the copper tape could accidentally connect because they are so close. That's why, for the fifth and final iteration, I made it so those wires stay opposite each other. This is the result of making this design:

Finally, I had to make sure it would work. So I hooked it up to my Arduino setup and checked. It worked like a charm!

This is the code used for the velostat sensor + LED:

/*
  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 ledPin = 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

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(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT);
}

void loop() {
  // 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: <100 wanneer niet ingedrukt en 1024 wanneer volledig ingedrukt.



     
//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, 950, 1024, 0, 255); // 100 voor minimum werkt niet; 500 ook niet. 950 geeft een goede geleidelijke verhoging van lichtintensiteit 

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

    // turn the ledPin on using the mappedvalue from the sensorpin
    analogWrite(ledPin, mappedValue);
}

During this week, I discovered that you can do a lot with an Arduino. It really got me excited to experiment a lot more with this little thing in the future!

Last updated