Here is what you will need to complete the Arduino side of the project:
Parts Required:
- PhotoCell (or PhotoResistor)
- 10K resistor
- Breadboard
- Arduino UNO
- 3-4 wires(to connect it all together)
- USB cable to upload sketch and for Serial communication with Processing.
Fritzing sketch:
Arduino Sketch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /* ========================================================== Project : Send Photo resistor values to computer Author: ScottC Created: 25th June 2011 Description: This sketch will make the arduino read Photo resistor values on analog pin A0. The analog readings will be dependent on the amount of light reaching the sensor. The Analog readings will be sent to the computer via the USB cable using Serial communication. ============================================================== */ int photoRPin = 0; int minLight; //Used to calibrate the readings int maxLight; //Used to calibrate the readings int lightLevel; int adjustedLightLevel; void setup() { Serial.begin(9600); //Setup the starting light level limits lightLevel=analogRead(photoRPin); minLight=lightLevel-20; maxLight=lightLevel; } void loop(){ //auto-adjust the minimum and maximum limits in real time lightLevel=analogRead(photoRPin); if(minLight>lightLevel){ minLight=lightLevel; } if(maxLight<lightLevel){ maxLight=lightLevel; } //Adjust the light level to produce a result between 0 and 100. adjustedLightLevel = map(lightLevel, minLight, maxLight, 0, 100); //Send the adjusted Light level result to Serial port (processing) Serial.println(adjustedLightLevel); //slow down the transmission for effective Serial communication. delay(50); } |
Arduino Sketch Explanation:
Processing Sketch
Here is a very basic Processing Sketch that will allow you to receive data from the Serial port attached to the Arduino and display the reading on your computer screen. In my case, the Arduino is connected to COM13. You may need to change this if you decide to follow in my footsteps.
The processing sketch will look for the line feed character ' \n' coming from COM13, which will trigger a serialEvent(). You can get processing to look for another character if you want to: just change the character within the bufferUntil() command. The draw() method is made redundant because the screen updating is handled by the serialEvent() in response to serial data communication with the Arduino UNO.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /* ============================================================ Processing Version: 2.2.1 Project: Display Sensor data on computer screen Author: ScottC Created: 25th June 2011 Description: This Processing sketch will allow you to display sensor data received from the Arduino via the serial COM port (or USB cable). =============================================================== */ import processing.serial.*; Serial myPort; String sensorReading=""; PFont font; void setup() { size(400,200); myPort = new Serial(this, "COM13", 9600); myPort.bufferUntil('\n'); font = createFont(PFont.list()[2],32); textFont(font); } void draw() { //The serialEvent controls the display } void serialEvent (Serial myPort){ sensorReading = myPort.readStringUntil('\n'); if(sensorReading != null){ sensorReading=trim(sensorReading); } writeText("Sensor Reading: " + sensorReading); } void writeText(String textToWrite){ background(255); fill(0); text(textToWrite, width/20, height/2); } |
This is how the data will be displayed on the computer screen:

I want to to connect simulink model of AUV to arduino ,sensors and actuators and want technical details of it
ReplyDeleteHi Anonymous,
DeleteSounds like an interesting project, but have never used Simulink, and have never heard of it until I saw your comment. Perhaps try the Arduino Forums and draw from collective intelligence. Sorry I could not help.
i want to use a Ledpin in it
ReplyDeleteHave a look at some of my other tutorials. There is one that uses an LED pin.
DeleteI'm trying to make a light sensing car, can I used what you have produced to do so?
ReplyDeleteI am sure you could
DeleteI am trying to set up multiple light sensors that will trigger sound. I'm using both arduino and processing.. Does anyone know what I should put into y code to allow this to happen? The bit that sorta says "if light sensor 1 reads value 0, trigger sound1'. I've tried this multiple times but doing it with more than one sensor doesn't seem to work. Hope someone can help or direct me where else to look!
ReplyDeleteHi Jennifer,
DeleteWithout knowing exactly what "doesn't seem to work", it is very hard to help you.
If you post your code to the ArduinoBasics Forum, I can have a look and will do my best to make recommendations. Make sure to click the "Help me with my project" link before adding a new topic to the forum.
Hey Scott,
ReplyDeleteMy serial port is COM3, so I changed the sketch to: myPort = new Serial(this, "COM3", 9600); but I keep receiving an error of 'variable or field 'serialEvent' declared void. Any suggestions?
Please post your code to the ArduinoBasics Forum
Deleteand I will have a look
I have the same problem.
DeleteAs above
DeleteDo the first and the second set of codes have to be together or they work separately? i just did a copy paste for a fast try and changed the port to COM1. something in the second set of codes do not work.
DeleteThe first set of code is entered into the Arduino IDE and uploaded to your Arduino Board. This code can work independently of the Processing sketch. You just have to open the Serial monitor to have a look at the data being sent by the Arduino.
DeleteThe second set of code is a Processing sketch. You need to enter this sketch into the Processing IDE which is compiled and run on your computer (not uploaded onto the Arduino). The processing sketch allows you to view the sensor reading as displayed in the picture above.
There is also a possibility that the processing code may not work with the latest version of Processing... but should work with version 2.2.1
DeleteSome modification may be required to work with the latest version of Processing, but I have not used the latest version to test this out. This tutorial was created back in 2011.
thank you, i will continue trying.
DeleteHi I want to make a model which will start rotating once any one wave hand in front of it (using proximity sensor ) and the motor would stop at the point where the light sensor will detect black color and as soon as the motor stops a video start playing on the other side and again as the video stops the motor starts rotating
ReplyDeleteHow can I update the Processing code for this to work in 3.0.1? Anybody have any ideas?
ReplyDeletehi, I am trying to use photocell conductor to detect different color light. do someone has an idea about it?
ReplyDeleteI don't think it will detect different colours (if you are talking about the common photocells - like the one in this tutorial).
DeleteYou can however, use an LED to detect a specific wavelength of light, but that is a bit different... I have a couple of tutorials which show how this is done, but they are quite old now, and there are probably much better ways of doing this now.
Hi Scott, where might I find your tutorials of using LED as detectors?
DeleteHi Mike:
DeleteI have a number of tutorials based around this concept.
The tutorials are quite old - so I hope they still work with the current version of Processing and Arduino IDE.
Here are some links:
Arduino Uno LED sensor Part 1
Arduino UNO - LED sensor Part 2<
And then I have a more complicated tutorial which involves the use of a Neural Network to detect colour using LEDs as detectors.
Colour Detection - the concept
And you can follow that to get to the Neural network tutorials.
Hope that helps.
completely new at electronics, the 10k resistor for the servo, what wattage should it be ?
ReplyDeleteDepends on your servo, however if it is a hobby servo like the one I have, then a standard 1/4W should be fine.
DeleteLike these: https://www.adafruit.com/product/2784
Is possible to program the system to activate a command when it receive a specific wavelenght?
ReplyDeleteDepends on the LEDs used.
DeleteBut you could line up a number of different coloured LEDs and use thresholds for each LED (or LED combination) to trigger a command.
Hello,
ReplyDeleteI would like to connect multiple photocell (photoresistor, light sensor) (4) on one board UNO ? And read individual values, how it can work ? Do you have any idea, I am quite new on Arduino. But I guess it's easy for you... Could you help me with this ?
check forum for getting help with your projects.
Delete