# How to connect and "read" two light sensors (photoresistors)

**URL:** <https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [August 14, 2020, 1:58pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254 "2020-08-14T13:58:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirzohid](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mirzohid](https://discourse.processing.org/u/Mirzohid)\
**Post date:** [August 14, 2020, 1:58pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/1 "2020-08-14T13:58:55Z")

</div>

Hi, everyone I wanted to connect 2 photoresistors and creat 2 rectangles which reflects lighting on that photoresistors seperatly.  
1 st question is is it even possible 'cause i couldn’t find anything about it on the internet

There is original code only for 1 photoresistor which worked just perfectly:  
Example 2B: Light sensor (Processing)  
(from [https://processing.org/tutorials/electronics/](https://processing.org/tutorials/electronics/) )

That is what i tryed (I just created another object from Serial class), but it says  
`port1 = new Serial(this, "COM3", 9600);`  
on this line that “COM3” is busy  
There is my whole code  
2 nd question is: if it is possible to connect and read 2 ports what i did wrong  
thank you

````auto
// Read data from the serial port and assign it to a variable. Set the fill a 
// rectangle on the screen using the value read from a light sensor connected 
// to the Wiring or Arduino board 
 
import processing.serial.*; 
 
Serial port; // Create object from Serial class
Serial port1;

int val; // Data received from the serial port 
int val1; 
void setup() { 
  size(400, 400); 
  noStroke(); 
  frameRate(10); // Run 10 frames per second
  // Open the port that the board is connected to and use the same speed (9600 bps) 
  port = new Serial(this, "COM3", 9600);
  port1 = new Serial(this, "COM3", 9600);
} 

void draw() { 
  if (0 < port.available()) { // If data is available to read,
    val = port.read(); // read it and store it in val
  } 
  background(204); // Clear background
  fill(val); // Set fill color with the value read
  rect(50, 50, 100, 100); // Draw square

  if (0 < port1.available()) { // If data is available to read,
    val1 = port1.read(); // read it and store it in val
  } 
      
  fill(val1); // Set fill color with the value read
  rect(200, 50, 100, 100); 
} ```
````

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [August 14, 2020, 2:44pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/2 "2020-08-14T14:44:40Z")

</div>

Hi @Mirzohid

Regarding your first question, yes it is possible. You either send the two values on the same Serial port or you use two Serial Ports each sending the data from one photoresistor. It depends on your electronic setup.

If you choose to send the two photoresistors state over the same Serial port, you can then, split the information and assign them to your variables.  
This is a quick example that I have found in a processing forum to illustrate.  
([Receive Multiple Sensor Information from Arduino to Processing - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/6241/receive-multiple-sensor-information-from-arduino-to-processing))  
There are multiple ways to do send the data. So, take the time to investigate as well 🙂  
Since your values go from 0 to 255 (I am guessing that you are using analogRead), then you can send one byte per photoresist ([byte / Reference / Processing.org](https://processing.org/reference/byte.html)), or as Strings in the example above.

If you choose to connect two serial ports, then you will have to identify which port is sending the values. So, you will need to create two Serial Port Objects, assign the Ports and baud rate, and on the Serial Event identify which one is sending data.  
Something like this,

```auto
import processing.serial.*; 
 
Serial portOne; // Create object from Serial class
Serial portTwo;

void setup() { 
  size(400, 400); 
  //Make sure that you are not assign the same COM port to the two Serial objects. Also, the baud rate should be the same!
  portOne = new Serial(this, "COM3", 9600);
  portTwo = new Serial(this, "COM4", 9600);
}
void serialEvent(Serial thisPort) {
 
  if(thisPort==portOne){
      //...GOOD NEWS: data from serial 1
  }
 
  if(thisPort==portTwo){
      //...GOOD NEWS: data from serial 2
  }
}

```

Once again, take the time to look for resources 🙂  
Below are some links that I think will be helpful to you in the future

> **Handshake Protocol**
>
> ([Connecting Arduino to Processing - SparkFun Learn](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all))

> **Split**
>
> [split() / Reference / Processing.org](https://processing.org/reference/split_.html)  
> [https://www.youtube.com/watch?v=PjxbuSnj8Pk](https://www.youtube.com/watch?v=PjxbuSnj8Pk)

Best regards!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 14, 2020, 3:58pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/3 "2020-08-14T15:58:20Z")

</div>

> [@Mirzohid](#):
>
> is it even possible 'cause i couldn’t find anything about it on the internet

It is possible and there are numerous resources out there on the Internet and in this forum.

One of the best tools in a programmer’s tool chest is knowing the resources available to you and learning to navigate, filter, and use them.

A short list of resources to peruse:

> **Resources \< Click here to expand !**
>
> Explore the resources available here:
> 
> - The Processing _website_ has references, examples, tutorials, links to resources, etc.  
> [https://processing.org/](https://processing.org/)
> 
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the menu bar will reveal what is available; libraries, tools, local examples, and other goodies are in there.
> 
> - The Coding Train \< Experience the unbridled enthusiasm of Daniel Shiffman  
> [https://shiffman.net/](https://shiffman.net/)  
> [https://www.youtube.com/user/shiffman](https://www.youtube.com/user/shiffman)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> 
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> 
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)
> 
> - Explore Processing on GitHub:  
> [Processing Foundation · GitHub](https://github.com/processing)  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java) \< Many of the Processing functions are here!
> 
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the menu.  
> In the education menu, there is a reference to the Coding Train.
> 
> - And the Internet. `:)`

`:)`

---

<div class="post-metadata">

**Author:** ![Mirzohid](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mirzohid](https://discourse.processing.org/u/Mirzohid)\
**Post date:** [August 15, 2020, 12:15pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/4 "2020-08-15T12:15:53Z")

</div>

Thank you a lot for responding and giving me a valuable suggestions, i will try them on. Also I have only one Arduino is sending info throught two ports good ? This is my electronic setup: [https://www.arduino.cc/en/uploads/Tutorial/PhotoCellA0.png](https://www.arduino.cc/en/uploads/Tutorial/PhotoCellA0.png)

---

<div class="post-metadata">

**Author:** ![Mirzohid](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mirzohid](https://discourse.processing.org/u/Mirzohid)\
**Post date:** [August 15, 2020, 12:18pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/5 "2020-08-15T12:18:37Z")

</div>

Alrigth, i appreciate ur recommendation and keep in mind what you said. Thank you.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 15, 2020, 12:26pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/6 "2020-08-15T12:26:31Z")

</div>

Hello,

Serial communications is easy\* for me but can be daunting for the uninitiated and requires a learning curve.

Easy\* from experience on other platforms but I had to adapt to understanding the Java implementation.

The Processing tutorial gives the simplest of examples only for only a byte.

Do a search in Google with a few appropriate keywords and an answer will be forthcoming. Search for:

> Processing Arduino serial

and filter through results.

Even a search in this forum provides some examples.

Do some exploration and come back when you have done some research.

Don’t assume everything will always work out of the box for your hardware or setup…

I had to “adapt” code from the Processing examples to work with my hardware:  
[https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/5](https://discourse.processing.org/t/solved-problems-sending-data-from-processing-to-arduino-through-serial-port/20138/5)

We are here to help when you hit a wall.

`:)`

---

<div class="post-metadata">

**Author:** ![Mirzohid](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mirzohid](https://discourse.processing.org/u/Mirzohid)\
**Post date:** [August 15, 2020, 12:31pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/7 "2020-08-15T12:31:37Z")

</div>

Ok, I will do it 😄

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 15, 2020, 12:59pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/8 "2020-08-15T12:59:29Z")

</div>

Hello,

Which part of the solution above was a solution for you?  
It will help others that are searching the forum to know.

Please send a quick response with a reference to what was a solution for you.  
I ask only because you have questions about two ports.

> [@Mirzohid](#):
>
> Also I have only one Arduino is sending info throught two ports good ?

Can you please elaborate? It helps to understand what you want to do with the additional port.  
Which Arduino are you using?

I typically use the USB serial port and add additional serial ports (see details below) as required; usually for testing or debugging so I can also use the serial monitor AND send data (two different ports each with its own purpose).

My Arduino Mega 2560 R3 has 6 pins for 3 additional hardware serial connections.  
The Arduino Uno DOES NOT have additional hardware serial pins and you will have to implement a [SoftwareSerial](https://www.arduino.cc/en/Reference/softwareSerial) (\< link!) with digital pins if you choose this path.

You will also require a USB to serial adapter for the additional serial pins if connecting to PC:  
[https://www.pololu.com/product/391](https://www.pololu.com/product/391) Just one example of this.

I use this one:  
[https://www.pololu.com/product/3172](https://www.pololu.com/product/3172)The programmer also features a TTL-level serial port.  
[https://www.pololu.com/docs/0J36/6](https://www.pololu.com/docs/0J36/6)

`:)`

---

<div class="post-metadata">

**Author:** ![Mirzohid](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mirzohid](https://discourse.processing.org/u/Mirzohid)\
**Post date:** [August 17, 2020, 2:31pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/9 "2020-08-17T14:31:25Z")

</div>

I wanted to ask is it available to conntect 2 port to 1 arduino, about solution one i will do it quite later and send you both link (hopefully if it work properly)  
With respect Mirzohid.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 17, 2020, 2:47pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/11 "2020-08-17T14:47:23Z")

</div>

> [@Mirzohid](#):
>
> I wanted to ask is it available to conntect 2 port to 1 arduino,

I answered that already.

`:)`

---

<div class="post-metadata">

**Author:** ![Mirzohid](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mirzohid](https://discourse.processing.org/u/Mirzohid)\
**Post date:** [August 17, 2020, 5:38pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/12 "2020-08-17T17:38:45Z")

</div>

Well, i use a arduino usb " USB 2.0 CABLE TYPE A/B" and arduino UNO. Also I am kinda newbie in arduino.

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [August 18, 2020, 7:40am UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/13 "2020-08-18T07:40:46Z")

</div>

So, I think @glv as already answered.  
If you need further help post here or in the arduino forum 🙂  
([https://forum.arduino.cc/](https://forum.arduino.cc/))

---

<div class="post-metadata">

**Author:** ![Mirzohid](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mirzohid](https://discourse.processing.org/u/Mirzohid)\
**Post date:** [September 18, 2020, 2:19pm UTC](https://discourse.processing.org/t/how-to-connect-and-read-two-light-sensors-photoresistors/23254/14 "2020-09-18T14:19:21Z")

</div>

@glv hello, I said that I will send you link that answer of question. Well, if you interested there is link to my project: [https://www.youtube.com/watch?v=THG-7JXbuJM](https://www.youtube.com/watch?v=THG-7JXbuJM)
