UV Light Sensor

by Meredith Barber in Circuits > Arduino

342 Views, 0 Favorites, 0 Comments

UV Light Sensor

IMG-6728.PNG
Analog UV Sensor

This device was created as a final project for an Interactive Electronic Art class. Our prompt was to create a device that expands our senses.

I wanted my final project to be personal to my family and myself. The idea of building a sensor that detects harmful UV light came from my grandfather, who recently passed away due to stage 4 melanoma.

The sun has always been a problem for my family members and I, and skin cancer runs in my family, so I am at a much higher risk. I believe that a smallwearable device that can sense harmful UV rays before your skin does would be very helpful and informative.

The prototype is based off of something that my grandfather would be able to use. I tried to think about something that an elderly person would be able to wear and understand. The idea is for the analog UV light sensor to detect a high UV ray, and send that information to a vibration motor. The device would buzz your wrist/arm when the UV index gets too high to let you know to apply some sunscreen.


*Disclaimer*

This device is just a prototype for a class. It isn't "wearable" yet, and the code could be improved, so feel free to add onto this device and make it your own.

Supplies

  • Analog UV Light Sensor (I bought 2, just in case, because it's pretty small and can be difficult to solder)

https://www.amazon.com/Adafruit-1918-Analog-Sensor-Breakout/dp/B00LEVTOB0/ref=sr_1_1?crid=28POWWDHFC653&keywords=adafruit+1918+analog+uv+light+sensor+breakout+-+guva-s12sd&qid=1682428125&sprefix=adafruit+analog%2Caps%2C100&sr=8-1

  • Vibration Motors (you probably won't need an entire 10 pack, but these wires are tiny and I wanted to be prepared for mistakes)

https://www.amazon.com/dp/B0B4SMWM52?ref=ppx_yo2ov_dt_b_product_details&th=1

  • 3 M to F Wires
  • 2 M to M Wires
  • Wire Cutter
  • Wire Stripper
  • Shrink Tube of varying sizes
  • Heat Gun
  • Soldering Kit (It's a good idea to already know the basics of soldering)
  • Arduino UNO R3
  • Computer with Arduino program

Get Your Sensor Soldered

IMG-6551.jpg
IMG-6729.jpg
IMG-6730.jpg

For this first step, you're going to want to attach your pins to the UV sensor and solder them together. This sensor came with 6 pins all stuck together, so I broke 3 apart from the rest. Then, you're going to want to place them so that the longer ends of the pins are sticking out of the back of the sensor. The second picture shows the side view of the pins in the sensor to tell which way to place them.

Once you have the pins facing the correct way, you are ready to solder! We stuck the pins in a breadboard to hold the sensor steady while we worked. I recommend using the smallest attachment that your solder has, because these pins are tiny, but you only have to do three. The third picture shows what the soldered pins should look like from the front of the sensor when you are done.

Get the Vibration Motor Ready

IMG-6553.jpg
IMG-6555.jpg
IMG-6554.jpg

The wires on the vibration motor are really small, so in this step, we secured them by soldering and using shrink tube. First, use some wire strippers and carefully expose a little bit of the wire on the vibration motor. Then take 2 M to M wires and cut and strip them the same way, so that both the M to M and vibration motor wires are exposed and can lightly twist together. It's a good idea to use red wire for power and black for ground.

Once you have your exposed wires twisted together (make sure the power and ground wires match up!) You can take your soldering iron and solder them together so they are connected. You basically just move the solder and the iron up and down the wire until the solder transfers and holds the wires together. For extra protection, we added a shrink tube to the area of the connected wires to make sure they don't break apart. The second image shows a close up of the connection.

Prepare the Connection Wires for the Sensor

IMG-6731.jpg
IMG-6733.jpg

This step isn't technically necessary for the project, but it really makes it easier to get the senor connected and the wires in place. We took 3 M to F wires and added shrink tube around the F end to hold them all in place. We used red wire for power, black for ground, and white for the middle sensor.

The most important step here is to make sure you have your wires in the right place when you go to connect them. The order should be black, white, red if you are connecting them and looking at it from the front of the sensor. The only thing we did here was make sure they were in the right order and lined up properly, and then we slipped a shrink tube over them and used the heat gun to shrink it down and hold them all in place.

Now all of your wires and connections are complete and easy to assemble and you can start developing the code!

Check Your Vibration Motor

To begin, I started with the basics: simply making sure the vibration motor worked. Instead of writing out code to check it, I used the Blink code that comes with the Arduino program. Blink is technically used to test a blinking LED, but you can use a vibration motor the same way. Attach the power wire to digital pin 13, and the ground wire to ground. Then, use the code that was already written and upload to test. Your vibration motor should be pulsing on and off, and that's how you know it works!

Downloads

Start the Code for the Sensor

code1.jpg
code2.jpg
code3.jpg

I actually started the code for the UV sensor by searching the internet to copy someone else. I found this website, thePiHut, where a comment underneath the product being sold had the starting code.

Website: https://thepihut.com/products/adafruit-analog-uv-light-sensor-breakout-guva-s12sd

This was the code that was written:

In order to get it work make sure to divide the output by the ADC of your MCU and multiply with the maximum voltage in mv.

Example in Arduino:


float sensorValue = analogRead(A0);

float sensorVoltage = sensorValue / 1024 * 3.3;

float UV_index_float = sensorVoltage / 0.1;

int UV_index_rounded = round(UV_index_float);


You'll want to declare all your floats at the top of the page. Right now, we are just focusing on the sensor, so lines like "pinMode" that you see in the second image will come in the next step. At this point, we just wanted to test if the sensor would read, and with this code, we were able to open the serial monitor on Arduino and see the numbers that the sensor was getting once in contact with UV light. (You'll probably have to step outside to check)

Add If Statements for the Vibration Motor

full-code.jpg

The included image and file is the full code so far. You'll want to add an If statement to tell your motor when to vibrate.

if(sensorValue >= 40){

  digitalWrite(6,HIGH);

  delay(1000);

That piece of code uses the analog value to read how high the UV is, and then tells the vibration motor to buzz if it's too high. My threshold is a UV index of 6, which poses a moderate risk, but you can change this number to whatever you chose at the top on the float threshold line.

An If Else statement tells your motor to stop vibrating if the analog sensor isn't reading a value that reaches the threshold. Here is what this line looks like:

}else{

  digitalWrite(6,LOW);

}


Connect to the UNO R3

IMG-6734.jpg
IMG-6735.jpg
IMG-6736.jpg

To connect your sensor to the Arduino UNO R3, start by connecting the F wires into the sensor, as shown in the first image. Make sure your power and ground matches up with the + and - signs on the sensor.

Once that is connected, place the black wire into one of the ground ports on the analog side of the UNO. Put the white wire into the A0 analog input port, and then the red wire into the 3.3V power port. Make sure you connect the red wire last.

Next, you can connect the vibration motor. Place the black wire into the ground port on the digital side of the UNO. Our code tells us we are using digital pin 6, so place the red wire into the 6 port.

Upload the Code and Use the Sensor

code5.jpg
Analog UV Sensor

Now you can plug your UNO R3 into your computer using the USB port. I always verify my code to make sure there are no problems. One it's verified, you can upload the code and open the serial monitor to see the output the sensor is reading.

Place the sensor in the sunlight to get a vibration, and place it in the shade to stop the vibration. You may have to adjust your threshold to a lower number if the UV index isn't very high, but once you know it works, you can change it to only vibrate when the UV index is at the level you want. My serial monitor output is reading 0 in the image because I was inside at the time.

When tested outside however, you can see the vibration motor going on and off when transferred from sunlight to shade.