[Problem] Stepper motor and RGB light sensor

Hi ! My trying to control the stepper motor with ISL29125 RGB light sensor. When the light sensor detect black color, the motor will rotate but when light sensor detect other color, the motor will stop.

The materials that I used are ISL29125 RGB light sensor, stepper motor, esp32, and l9110s h-bridge stepper motor dual dc driver controller board.

BUT I try this code but it didn't work. Moreover, when I run this it appear nothing on serial monitor. After that Arduino program is stop working and cannot do anything.

#include <Wire.h>
#include "SparkFunISL29125.h"
#include <Stepper.h>

SFE_ISL29125 RGB_sensor;

const int stepsPerRevolution = 200; 

Stepper myStepper(stepsPerRevolution, 14, 12, 13, 15);

int stepCount = 0;  

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

void loop()
{
  myStepper.step(stepsPerRevolution);
  delay(500);

  unsigned int blue = RGB_sensor.readBlue();
  if((unsigned int) blue < 10000){
      myStepper.setSpeed(30);
      Serial.println("black");
      delay(2000);
    }
  else 
  {
  myStepper.setSpeed(0);
  Serial.print(blue,DEC);
  Serial.println();
  }
  delay(2000);
}

Code by itself tells us little. We need a schematic (not a Fritzing thing) to see how it is wired up.
Also what Arduino are you using and have you tried printing something in the setup function to see if that works on your monitor, which must be set to the same speed as your software.

Hi, @chayapitcha_sae
Have you got code for each of your input and output devices to prove you can read and control them on their own?

So have you code;

  1. That just reads the light sensor and works?
  2. That just controls the stepper and works?
  3. That just communicates via the com port?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

#include <Wire.h>
#include "SparkFunISL29125.h"
#include <Stepper.h>

SFE_ISL29125 RGB_sensor;

const int stepsPerRevolution = 18; 

Stepper myStepper(stepsPerRevolution, 14, 12, 13, 15);

int stepCount = 0;  

void setup()
{
  myStepper.setSpeed(30);
  Serial.begin(115200);
  
}

void loop()
{
  
  unsigned int blue = RGB_sensor.readBlue();
  
  if(blue < 10000)
  {
      myStepper.step(stepsPerRevolution);
      Serial.println("black");
      delay(2000);
  }
  else 
  { 
    Serial.print(blue,DEC);
    Serial.println();
  }
  delay(2000);
}

I try this, and it doesn't work

Hi, @chayapitcha_sae

What do you mean, doesn't work.
Did it do anything?
What did you see in the IDE monitor?
You have got the monitor set to 115200 baud?

I ran your code on an ESP32 and got reset messages in the monitor?

Tom... :grinning: :+1: :coffee: :australia:

Hi,
Look at the examples for the Sparkfun Library.
You are missing some lines in the void setup() function
Also this line in the description of the stepper.h library.

This library is compatible with all architectures so you should be able to use it on all the Arduino boards.
ESP32 is not Arduino board.

I found the code did not like this line.

 myStepper.setSpeed(0);

But if I changed it to;

 myStepper.setSpeed(1);

It stopped resetting, butr with no hardware connected I could not deduce if the code was actually reading the sensor, or controlling the stepper.
Did you read the three points in post#3 ?
Have you got code for each of the sensor and stepper onn their own to prove the ESP can read/write to them.

A schematic diagram would be helpful.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Yes you said that before.
And I said

Are you having trouble with English?

First, before I combine sensor and motor, I write the code and it work.

Code for sensor

#include <Wire.h>
#include "SparkFunISL29125.h"


SFE_ISL29125 RGB_sensor;

void setup()
{
  
  Serial.begin(115200);

  if (!RGB_sensor.init())
  
  {
    Serial.println("Error");
  }
}

void loop()
{
  //Read light intensity of blue channel
  unsigned int blue = RGB_sensor.readBlue();
  if((unsigned int) blue < 10000){
      Serial.println("black");
      delay(2000);
    }
  else 
  {
  Serial.print(blue,DEC);
  Serial.println();
  }
  delay(2000);
}

The result: when RGB light sensor detect light intensity below 10000, it will show black in serial monitor.

Code for stepper motor

#include <Stepper.h>

const int stepsPerRevolution = 18; 

Stepper myStepper(stepsPerRevolution, 14, 12, 13, 15);

int stepCount = 0;         // number of steps the motor has taken

void setup() {
   myStepper.setSpeed(30);
   Serial.begin(115200);
}

void loop() {
  Serial.println("channel1");
  myStepper.step(stepsPerRevolution);
  delay(1500);
  
}

The motor wil rotate and stop for a sec that I set.

BUT. After I combine all of this, the motor start rotate but the RGB light sensor cannot detect anythings in serial monitor.

#include <Wire.h>
#include "SparkFunISL29125.h"
#include <Stepper.h>

SFE_ISL29125 RGB_sensor;

const int stepsPerRevolution = 18; 

Stepper myStepper(stepsPerRevolution, 14, 12, 13, 15);

int stepCount = 0;  

void setup()
{
  myStepper.setSpeed(30);
  Serial.begin(115200);
  RGB_sensor.init();
}

void loop()
{
  
  unsigned int blue = RGB_sensor.readBlue();
  
  if(blue < 10000)
  {
      myStepper.step(stepsPerRevolution);
      Serial.println("black");
  }
  else 
  { 
    myStepper.setSpeed(0);
    Serial.print(blue,DEC);
    Serial.println();
  }
  delay(2000);
}

Moreover, I set the serial monitor at the same speed in the code.

My latest code

#include <Wire.h>
#include "SparkFunISL29125.h"
#include <Stepper.h>

SFE_ISL29125 RGB_sensor;

const int stepsPerRevolution = 18; 

Stepper myStepper(stepsPerRevolution, 14, 12, 13, 15);

int stepCount = 0;  

void setup()
{
  myStepper.setSpeed(30);
  Serial.begin(115200);
  RGB_sensor.init();
}

void loop()
{
  
  unsigned int blue = RGB_sensor.readBlue();
  
  if(blue < 10000)
  {
      myStepper.step(stepsPerRevolution);
      Serial.println("black");
  }
  else 
  { 
    myStepper.setSpeed(1);
    Serial.print(blue,DEC);
    Serial.println();
  }
  delay(2000);
}

Result: The motor not rotate and the RGB light sensor not show the result.
The serial monitor is show like this.

Seriously? It is very odd how you are getting output to the monitor that is not in your code. How is that happening?

Are you sure you only have 18 steps per revolution, this seems a very low number for a motor.

Finally your physical layout diagram (I actually asked for a schematic) shows no pull up resistors on the two I2C signal lines. You should fit at least a 3K3 resistors.

Hi ! My problem is almost solve. The motor can rotate when RGB light sensor detect the light intensity below 10000 and the motor will stop when the RGB light sensor detect light intensity above the 10000.

Here is my code.

#include <Wire.h>
#include "SparkFunISL29125.h"
#include <Stepper.h>

SFE_ISL29125 RGB_sensor;

const int stepsPerRevolution = 200; 

Stepper myStepper(stepsPerRevolution, 14, 12, 13, 15);

int stepCount = 0;  

void setup()
{
  Serial.begin(115200);
  RGB_sensor.init();
}

void loop()
{
  
  unsigned int blue = RGB_sensor.readBlue();
  
  if(blue < 10000)
  {
      myStepper.step(1);
      Serial.println("black");
  }
  else 
  { 
    myStepper.step(0);
    Serial.print(blue,DEC);
    Serial.println();
  }
  delay(2000);
}

BUT can you recommend about the speed. I really want to increase the speed. How can I do that ? about stepsPerRevolution or myStepper.step();

You seem to have missed the set speed call in your setup. You had it in in previous codes you posted.

myStepper.setSpeed(30);

The bigger the number the faster it will go. There is a limit to this however, if you try and go too fast then it will not move at all, so see what the biggest number you can use and still have it move.

Thank you so much. My code is already work. THANK YOU

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.