# Problem with stepper motor and photodiode

Hi all,
I encountered a strange problem with a stepper motor driving an optical grid and measuring the corresponding output of a photodiode.
I have an old spectrophotometer. There is a stepper motor driving the optical grid. A photodiode mesaures the output of the whole thing.
The output should be as follows: it should rise until about 510 nm, then it should fall again. In principle this happens, but when you look nearer to the curve of the output, there are small, regular oscillations (see picture (blue symbols), which shows a small part of the curve).

This is my code, running on an Uno:

``````//Schrittmotorsteuerung für 28BYJ-48 5VDC mit ULN2003A
// https://www.mikrocontroller.net/topic/333820

#define NrOfMotorOutputs 4 //Anzahl der Ausgänge zur Ansteuerung des Motors
#define NrOfPatternsOneStep 8 //Anzahl der Muster zur Ansteuerung eines Schritts

//Variabeln für Drehrichtung definieren
const byte MotorOff        =  0;
const byte DirMotorRight   =  2;
const byte DirMotorLeft    =  1;

const int lambdaEnd = 12;                           // Endswitch
int statusLambdaEnd = 0;
int n = 0;                                          // counter steps
const int LampeAnPin = A5;                          // switch lamp
const int ADWandlerIn = A0;                         // A/D

int val;
int lambda;

byte ActStepNr = 0;                                 // momentary step
//Schrittmuster definieren

byte StepPattern[NrOfPatternsOneStep][NrOfMotorOutputs] =
{

{1, 1, 0, 0},                                      // from: https://www.kollino.de/arduino/schrittmotor-28byj-48/
{0, 1, 0, 0},                                     // NrOfPatternsOneStep 8
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 0}
};

// Motor Pins for output 1-4
byte MotorOutputPin[NrOfMotorOutputs] = {8,10,9,11}; //Change of sequence: https://blog.thesen.eu/es-geht-doch-schrittmotor-28byj-48-mit-der-original-arduino-stepper-library/

void setup()
{
pinMode(lambdaEnd, INPUT_PULLUP);
pinMode(LampeAnPin, OUTPUT);

for (byte i=0; i < NrOfMotorOutputs; i++)
{
pinMode((MotorOutputPin[i]), OUTPUT);
digitalWrite(MotorOutputPin[i], LOW);
}
do
{
MotorMove(DirMotorRight,10,1);
}
while(statusLambdaEnd == HIGH);

Serial.begin(9600);
}

void loop()
{
digitalWrite(LampeAnPin, HIGH);

for (n=0; n<943; n++)
{                                        //942 steps corresponds with the whole spectrum
lambda = (n*0.628)+314;
Serial.print(lambda); Serial.print(","); Serial.print(val); Serial.print(","); Serial.println(n);
MotorMove(DirMotorLeft,50, 1);
}
digitalWrite(LampeAnPin, LOW);

while(1);                                   // program stop
}

void MotorMove(byte MotorDir, unsigned int MotorSpeed, unsigned int StepsToGo)
{

//Motor Stop all outputs LOW
if (MotorDir == MotorOff)
{
for (byte i=0; i < NrOfMotorOutputs; i++)
{
digitalWrite(MotorOutputPin[i], LOW);
}
}

//Motor right
else if (MotorDir == DirMotorRight)
{
for (int i=0; i < StepsToGo; i++)
{
ActStepNr++;
if (ActStepNr == NrOfPatternsOneStep) ActStepNr = 0;
for (byte j=0; j < NrOfMotorOutputs; j++)
{
digitalWrite(MotorOutputPin[j], StepPattern[ActStepNr][j]);
}
delay(MotorSpeed);
}
}

//Motor left
else if (MotorDir == DirMotorLeft)
{
for (int i=0; i < StepsToGo; i++)
{
if (ActStepNr == 0) ActStepNr = NrOfPatternsOneStep-1;
else ActStepNr--;
for (byte j=0; j < NrOfMotorOutputs; j++)
{
digitalWrite(MotorOutputPin[j], StepPattern[ActStepNr][j]);
}
delay(MotorSpeed);
}
}
}
``````

I have no idea what causes this problem. Have you?

-richard

Have you an independent means to confirm that the Arduino values really are wrong? Or. more specifically, to confirm that the sensor is producing correct values but the Arduino is not collecting those values properly.

…R

PS … it will make it easier for people to help you if you display your image here. See this Simple Image Guide

I see this: lambda = (n*0.628)+314;

Is a strange mixture of integers and floating point. Have you ever printed the actual values of lambda you get from this calculation?

Paul

I wanted an output with nothing after the decimal point. I changed it to Serial.print(lambda,0) with lambda as float.
@Robin2: Sorry, but I’m too dumb to insert the image. I edited the post with img-tags around the url (as outlined in the guide), but without success.

The correctness of the values I’ll measure on wednesday. Until then - thanks for your answers!

r_a_mueller:
@Robin2: Sorry, but I'm too dumb to insert the image. I edited the post with img-tags around the url (as outlined in the guide), but without success.

I don't think Google Drive allows your image to be displayed here. Just upload the image file as an attachment here and then follow my guide

...R

There might be interference from the motor circuit on the analog sensor. You haven't said much about how its wired up and powered.

Perhaps there's mains interference and you are seeing this imposed on the analog readings. If you take the readings precisely timed to match the mains frequency you can cancel this effect.