ofSerial combined with Arduino/LDR gives unwanted results

Hey everyone! I recently started with working in openFrameworks. For my project I connected a LDR to an arduino, which then writes the data from the LDR to the serial port. I try to read this data (and for now just print it) using the ofSerial class. I used the serialExample that is supplied in the examples folder with installing openFrameworks.

The thing is, it seems to be working. However, the data that I am getting in OF does not correspond at all with what I’m supposed to get (it works perfectly fine when reading directly from the Arduino in the arduino IDE, no sudden fluctuations, no out of range errors, etc.). The data in OF seems to be between 0-255 while the arduino writes it in 0-1023 format. Also there are heavy fluctuations in OF regardless of the light situation of the LDR.

Does anyone know how I could solve this?

Here is my code:


#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp{

	void setup();
	void update();
	void draw();

	void keyPressed(int key);
	void keyReleased(int key);
	void mouseMoved(int x, int y );
	void mouseDragged(int x, int y, int button);
	void mousePressed(int x, int y, int button);
	void mouseReleased(int x, int y, int button);
	void windowResized(int w, int h);
	void dragEvent(ofDragInfo dragInfo);
	void gotMessage(ofMessage msg);

	ofSerial	serial;  // Adds ofSerial class to the project.
	ofVideoPlayer myPlayer; //adds videoplayer class to the project



#include "ofApp.h"

void ofApp::setup(){
	vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();

	// this should be set to whatever com port your serial device is connected to.
	// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)
	// arduino users check in arduino app....
	int baud = 9600;
	serial.setup(0, baud); //open the first device
	//serial.setup("COM4", baud); // windows example for selecting specific comport
	//serial.setup("/dev/tty.usbserial-A4001JEC", baud); // mac osx example for selecting specific comport

void ofApp::update(){
    // If the byte size is bigger than 0 (ignores zeroes coming from the LDR)
    if(serial.available() > 0) {
        // Read the bytes from serial port
        int myByte = serial.readByte();
        // No data from serial
        if ( myByte == OF_SERIAL_NO_DATA )
            printf("no data was read");
        // If serial error
        else if ( myByte == OF_SERIAL_ERROR )
            printf("an error occurred");
        // If it actually is working.
        /*else if ( myByte > 130 ){
            printf("There's no light!");

            printf("myByte is %d", myByte);

void ofApp::draw(){

void ofApp::keyPressed(int key){


void ofApp::keyReleased(int key){


void ofApp::mouseMoved(int x, int y ){


void ofApp::mouseDragged(int x, int y, int button){


void ofApp::mousePressed(int x, int y, int button){


void ofApp::mouseReleased(int x, int y, int button){


void ofApp::windowResized(int w, int h){


void ofApp::gotMessage(ofMessage msg){


void ofApp::dragEvent(ofDragInfo dragInfo){


Arduino LDR code

int LDR_Pin = A0; //analog pin 0

void setup(){

void loop(){
  int LDRReading = analogRead(LDR_Pin); 

  /*Use Serial.write for communication with openFrameworks, 
  /use Serial.println for debugging the arduino itself*/
  delay(125); //just here to slow down the output for easier reading

Alternatively, my files are for download here.

Solved; the solution was to map the LDR data from 0-1024 to 0-255 with the map(); function in arduino.

1 Like