Introduction: Photoresistor Controlling Game Scene

I connected a Photoresistor to Unity and Unreal engine. Here you can adjust the brightness of a light in the scene of the respectable game engines. If the in real life surroundings are bright, the scene in the game will have a bright light and the same for darkness. If it's dark in real life, the light won't be very light.

Supplies

  • Arduino Uno
  • Breadboard
  • Photoresistor
  • 10kΩ Resistor
  • Arduino IDE
  • Unity or Unreal Engine (for unreal engine I used 4.27, newer versions can be tricky)

Step 1: Setting Up the Arduino

Follow the picture.

Step 2: Arduino Code

Add the following code to Arduino IDE:

const int lightSensorPin = A1;
int lightSensorValue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  lightSensorValue = analogRead(lightSensorPin);
  Serial.println(lightSensorValue);
}

Check if there's any output in the Serial Monitor and that it changes whether you cover it with your hand or shine a light on the photoresistor.

Step 3: Unreal Engine 4.27

1.      Open a new project.

2.      Download Unreal_Engine_SerialCOM_Plugin and add it to your project folder.

3.      You might need to restart the project for the Plugin to work.

4.      Add a light to your scene and make a blueprint onto it.


Add these to check if your port is open or closed. Press play and in the Outlog it shoulder write either, open or closed. If closed, check if your Serial Monitor is still running in Arduino IDE and close it, then try again in Unreal engine.

Create a custom event and call it Loop. Add the nodes like the image above.

Map Range Clamped works as the following:

·      IN Range A you put the lowest value the photoresistor can put out, which is 0.

·      In Range B you put the highest value the Photoresistor can put out, which is 1000.

·      Out Range A you put the lowest value you want your light to be, in my case, 0.

·      Out Range B you put the highest value you want your light to be, in my case, 100000.

Press play and you should get the numbers on your screen with the values of the photoresistor and the light should change whether it’s bright around the photoresistor or not.

Step 4: Unity

For Unity:

Open a new project and add a visible light to the scene.

Then add a new script, I called it LightControl and paste in the following code:

using UnityEngine;
using System;
using System.IO.Ports; //if this line of code causes trouble, then go to Edit > Project Settings > Player > Api compatibility level and change the input to .NET Framework


public class LightControl : MonoBehaviour //change filename if you put another name there
{
    // Variables for serial communication
    private string portName = "COM3"; // Adjust port name as needed
    private int baudRate = 9600;
    private SerialPort serialPort;


    public float intensityMultiplier = 100f; // Adjust this multiplier as needed


    void Start()
    {
        // Initialize serial port
        serialPort = new SerialPort(portName, baudRate);
        try
        {
            serialPort.Open();
        }
        catch (Exception e)
        {
            Debug.LogError("Error opening serial port: " + e.Message);
        }
    }


    void Update()
    {
        if (serialPort != null && serialPort.IsOpen)
        {
            try
            {
                // Read data from Arduino
                string data = serialPort.ReadLine();
                int lightIntensity = int.Parse(data);


                // Print input of photoresistor to console
                Debug.Log("Light intensity: " + lightIntensity);


                // Map light intensity to a range suitable for the light component
                float normalizedIntensity = Mathf.Clamp01(lightIntensity / 1023f);


                // Apply intensity to the light
                Light lightComponent = GetComponent<Light>();
                lightComponent.intensity = normalizedIntensity * intensityMultiplier;
            }
            catch (Exception e)
            {
                Debug.LogError("Error reading from serial port: " + e.Message);
            }
        }
    }


    void OnDestroy()
    {
        // Close serial port when the script is destroyed
        if (serialPort != null && serialPort.IsOpen)
        {
            serialPort.Close();
        }
    }
}


Put this script on your light object in the scene and press play.

Check in the console if it's putting out numbers.

Then select your light object and check the intensity, if it's really low, go to your script and change the Intensity Multiplier. Mine is set to 10.


Your light should now change based on the input from your Photoresistor.


If nothing happens, check if the serial monitor in Arduino IDE is still running, if so, close it, and restart the Scene in Unity.