1
\$\begingroup\$

enter image description hereI am relatively new to using Raspberry Pis and wiring sensors. I am trying to wire the PM-U25 photoelectric sensor to a Raspberry Pi 4 to act as an optical resolver that tracks a shutter wheel on a rotating motor. When I wire 1 sensor (+V to an external power source, -V to an external power source, Light ON to GPIO, Dark ON to GPIO) it works perfectly fine and I can read the output (I am only using the Light ON, so I technically don't need the Dark ON. But I've noticed I only get outputs with both plugged in...? Leaving only one connected to the RPI gives me no signal). However, when I wire both sensors up in the same manner (2 outputs to RPI), I don't detect any output from either sensor. Does anyone have a suggestion or know how I should be wiring up the sensors to the RPIs? Do I need to somehow connect the dark ON wires together?

Here is a link to the photoelectric sensor I'm using. The electrical schematic is on page 10 in the top right corner: https://www.onlinecomponents.com/en/datasheet/pmu25-49373297/

Thanks! Any advice would be greatly appreciated.

EDITS: I've attached a photo of my setup and a code snippet.

void motorController::initializeRoll()
{
    qDebug() << "Initialize roll() called!";
    QString motorUsed = "roll";
    setDirection(clockwise, directionPinNegRoll, directionPinPosRoll);
    gpioSetMode(sensorPinWRoll, 1);
    gpioSetMode(sensorPinRoll, 0);

    //gpioSetMode(15, 0);
    //gpioSetMode(14, 1);


    serverSocket rollInitializeErrorNotifier;
    QString data;
    rollSteps = 0;

    bool initSuccess = false;
    float initSpeed = getSpeed(3);
    int sensorRead;
    //int sensorReadb;
    int consecutiveZeroScan=0;
    int consecutiveOneScan=0;
    int minimumFlagLength = 2.83*gearRatio;//8*gearRatio; //8 for 400smooth (step/rev)
    int maximumFlagLength = 5.194*gearRatio;//Too many scans means something is wrong with sensor. //12 for 400 smooth
    int consecutiveZeroThreshold = 2.3611*gearRatio; //How many scans until we decide sensor isn't detecting anything. //10 for 400 smooth
    int maxOneCount = 0;
    int maxZeroCount = 0;
    int initializeStepsBeforeGiveUp = 1.2*stepsPerRev*gearRatio;


    for (int i = 0; i < initializeStepsBeforeGiveUp; i++) {
        sensorRead = gpioRead(sensorPinWRoll);
        //sensorReadb = gpioRead(sensorPinRoll);

        qDebug() << "sensorReadWhite value AT START is: " << sensorRead;
        //qDebug() << "sensorReadBlack value AT START is: " << sensorReadb;
        qDebug() << "MaxOneCOUNT is :                 " << maxOneCount;
        qDebug() << "MaxZeroCOUNT is :                 " << maxZeroCount;



        if (sensorRead == 1) { //sensor detection.

            consecutiveZeroScan=0;

            while (sensorRead == 1) {
                consecutiveOneScan++;
                qDebug() << "ConsecutiveOneScan is: " << consecutiveOneScan;


                //This code is helpful in determining where the flag is. Whatever has the higher count (OneScan vs ZeroScan) will be the flag.
                if (consecutiveOneScan>maxOneCount){
                    maxOneCount = consecutiveOneScan;
                }
                //End of commented help code.



                if (consecutiveOneScan == minimumFlagLength) {
                    initSuccess = true;

                }

                if (consecutiveOneScan == maximumFlagLength) {
                    qDebug() << "Error: Roll Initialization failure. Sensor failure - maxFlag threshold exceeded, Voltage too low OR shutter wheel damaged.";
                    data = "Error: Roll Initialization failure. Sensor failure - maxFlag threshold exceeded, Voltage too low OR shutter wheel damaged.";
                    rollInitializeErrorNotifier.sendUpdate(data);
                    emit destroyMotorThreadInitRoll();
                    return;
                }

                moveOneStep(initSpeed, stepPinNegRoll, stepPinPosRoll, motorUsed);
                sensorRead = gpioRead(sensorPinWRoll);
                qDebug() << "sensorRead value IN WHILE is: " << sensorRead;
                //                qDebug() << "sensorReadb value IN WHILE is: " << sensorReadb;

            }

            consecutiveOneScan = 0;


        }

        else { //Sensor doesn't detect and only 0s are returned.
            consecutiveOneScan = 0;
            consecutiveZeroScan++;
            //qDebug() << "ConsecutiveZeroScan is: " << consecutiveZeroScan;

            //This code is helpful in determining where the flag is. The Zero should be the flag but will also have the higher scans hit.
            if (consecutiveZeroScan>maxZeroCount){
                maxZeroCount = consecutiveZeroScan;
            }
            //End of commented help code.

            if (consecutiveZeroScan>=consecutiveZeroThreshold) {
                qDebug() << "Error: Roll Initialization failure. Sensor failure - nothing detected, Voltage too low OR shutter wheel damaged OR GPIO miswiring.";
                data = "Error: Roll Initialization failure. Sensor failure - nothing detected, Voltage too low OR shutter wheel damaged OR GPIO miswiring.";

                rollInitializeErrorNotifier.sendUpdate(data);
                emit destroyMotorThreadInitRoll();
                return;
            }
        }


        moveOneStep(initSpeed, stepPinNegRoll, stepPinPosRoll, motorUsed);

        if (initSuccess) {
            //qDebug() << "initSuccess at end was true. Emitting success";
            emit motorInitializeRollSuccess();
            emit destroyMotorThreadInitRoll();
            return;

        }

    }


    if (!initSuccess) { //main flag not detected after full rotation.
        qDebug() << "Error: Roll Initialization failure. Sensor failure - main flag not detected, Voltage too low OR shutter wheel damaged.";
        data = "Error: Roll Initialization failure. Sensor failure - main flag not detected, Voltage too low OR shutter wheel damaged.";

        rollInitializeErrorNotifier.sendUpdate(data);
        emit destroyMotorThreadInitRoll();
        return;
    }
    emit destroyMotorThreadInitRoll();

}

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Please provide your circuit, and a snipet of code showing how you're reading it. \$\endgroup\$
    – Drew
    Mar 5 at 20:02
  • \$\begingroup\$ @Drew Let me know if that works? I had to edit my main post because of the character limits on the comments. \$\endgroup\$ Mar 5 at 20:47
  • 1
    \$\begingroup\$ @dij0nmustard - Hi, You said: "I had to edit my main post because of the character limits on the comments.". FYI that is the right thing to do on Stack Exchange. Questions should be edited to clarify or add relevant information (unless there has already been a genuine answer attempt, based on the information given at that point, and the planned edit to the question would invalidate that answer - that should be avoided, and can indicate that the question is changing, rather than being clarified). \$\endgroup\$
    – SamGibson
    Mar 5 at 21:01

1 Answer 1

1
\$\begingroup\$

You must connect the negative side of your external power supply to the RPi Ground. You may also require pull-up resistors (10K or so) from the Pi input pins to +3.3 Volts.

The inputs will be Low when active.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.