Stepper Motor Significantly Slows Down With Lux Sensor Code

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
terennum
 
Posts: 6
Joined: Sat Apr 09, 2016 3:21 pm

Stepper Motor Significantly Slows Down With Lux Sensor Code

Post by terennum »

Stepper motor https://www.adafruit.com/products/324

Stepper motor driver DRV8825 https://www.pololu.com/product/2133

TSL2561 lux sensor https://www.adafruit.com/products/439

I am able to run the motor at 400 RPM via my Arduino Uno without any issues. However, if I add the example code, https://learn.adafruit.com/tsl2561/use, for the lux sensor, while the sensor feedback works fine, the motor slows all the way down to ~5 RPM. I deleted serial monitoring related code but it didn't help.

Thanks in advance!
Last edited by terennum on Sat Apr 09, 2016 7:56 pm, edited 1 time in total.

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by adafruit_support_bill »

Please post the code that you are using.

User avatar
terennum
 
Posts: 6
Joined: Sat Apr 09, 2016 3:21 pm

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by terennum »

adafruit_support_bill wrote:Please post the code that you are using.

Code: Select all

/* Pololu Stepper Motor Driver & Lux Sensor
 * 8 April 2016
 */
 
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include "BasicStepperDriver.h"
#define DIR_PIN 8
#define STEP_PIN 9
#define M0 10
#define M1 11
#define M2 12
#define MICROSTEPS 8
#define S 200

Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);

const int sensorPin = A0;
int STEPS = 1;
int LightThreshold = 100; // lux

/**************************************************************************/
/*
    Arduino setup function 
*/
/**************************************************************************/
void setup() {
  Serial.begin(250000);
  Serial.println("Light Sensor Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!tsl.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }
  
  /* Display some basic information on this sensor */
  displaySensorDetails();
  
  /* Setup the sensor gain and integration time */
  configureSensor();
  
  /* We're ready to go! */
  Serial.println("");

  /* Stepper Pins */
  pinMode(DIR_PIN,OUTPUT);
  pinMode(STEP_PIN,OUTPUT);

  /* Stepper Microstepping Pins */
  pinMode(M0,OUTPUT);
  pinMode(M1,OUTPUT);
  pinMode(M2,OUTPUT);
  
  /* Configure microstepping: 8 = H,H,L */
  digitalWrite(M0,HIGH);
  digitalWrite(M1,HIGH);
  digitalWrite(M2,LOW);
}

/**************************************************************************/
/*
    Arduino loop function
*/
/**************************************************************************/
void loop() {
  int sensorReading = analogRead(sensorPin);
  int RPM = map(sensorReading,0,1023,-400,400);
  double usDelay0 = 60*(1000000/2.0/MICROSTEPS/S/RPM);
  int usDelay = (int)round(usDelay0);

  /* Get a new sensor event */ 
  sensors_event_t event;
  tsl.getEvent(&event);
 
  /* Display the results (light is measured in lux) */
  if (event.light)
  {
    Serial.print(event.light); Serial.println(" lux");
  }
  else
  {
    /* If event.light = 0 lux the sensor is probably saturated
       and no reliable data could be generated! */
    Serial.println("Sensor overload");
  }

  if (event.light < LightThreshold) {
    rotate(0,0);
  }
  else {
    rotate(STEPS*MICROSTEPS, usDelay);
  }
}

/**************************************************************************/
/*
    Rotates stepper motor given amount and speed
*/
/**************************************************************************/
void rotate(int steps, float usDelay) {

  int dir = (usDelay > 0) ? HIGH:LOW;
  usDelay = abs(usDelay);

  digitalWrite(DIR_PIN,dir); 
  
  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
}


/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  tsl.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
  /* You can also manually set the gain or enable auto-gain support */
  // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
  
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

  /* Update these values depending on what you've set above! */  
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         "); Serial.println("Auto");
  Serial.print  ("Timing:       "); Serial.println("13 ms");
  Serial.println("------------------------------------");
}

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by adafruit_support_bill »

Your sensor is configured for a 13mS integration time. It looks like you are taking a reading per full-step. So at best (assuming no other overhead) it would take 13 * 200 = 2600 ms/revolution. Or about 23 RPM.

User avatar
terennum
 
Posts: 6
Joined: Sat Apr 09, 2016 3:21 pm

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by terennum »

adafruit_support_bill wrote:Your sensor is configured for a 13mS integration time. It looks like you are taking a reading per full-step. So at best (assuming no other overhead) it would take 13 * 200 = 2600 ms/revolution. Or about 23 RPM.
Thank you. Now I understand.

In that case is there a different way to implement the logic which would allow for a faster motor speed? Like an interrupt of sorts?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by adafruit_support_bill »

It would require some library hacking. It should be possible to initiate asynchronous reads and come back later to get the data. But there is no support for that in this library. All the reads are synchronous.

See the getData() function in the library: https://github.com/adafruit/Adafruit_TS ... 2561_U.cpp

User avatar
terennum
 
Posts: 6
Joined: Sat Apr 09, 2016 3:21 pm

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by terennum »

adafruit_support_bill wrote:It would require some library hacking. It should be possible to initiate asynchronous reads and come back later to get the data. But there is no support for that in this library. All the reads are synchronous.

See the getData() function in the library: https://github.com/adafruit/Adafruit_TS ... 2561_U.cpp
I appreciate your support on this. As a newbie I am not familiar with reading data asynchronously. Do you mind providing me with a source where I can learn more about it? Or give me an example code?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by adafruit_support_bill »

There are a few ways to approach it. But all of them would involve some re-structuring of your code and/or the library. If you can explain what it is you are trying to achieve, we might be able to suggest the most straightforward way to get there.

User avatar
terennum
 
Posts: 6
Joined: Sat Apr 09, 2016 3:21 pm

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by terennum »

adafruit_support_bill wrote:There are a few ways to approach it. But all of them would involve some re-structuring of your code and/or the library. If you can explain what it is you are trying to achieve, we might be able to suggest the most straightforward way to get there.
My objective is to run the stepper motor at a pre-specified speed (e.g. 400 RPM) when a certain lux value is exceeded (e.g. 200 lux). Pseudo code:

Code: Select all

IF LUX > 200
  START RUNNING THE STEPPER MOTOR at 400 RPM
ELSE
  STOP RUNNING THE STEPPER MOTOR
END
I attempted increasing the sensor reading interval via the simple logic below but every time this IF condition is executed, the stepper halts momentarily which is not really ideal.

Code: Select all

/* Get a new sensor event */ 
  if (millis() - lastTime > sensorReadInterval) {
    sensors_event_t event;
    tsl.getEvent(&event);
    luxReading = event.light;
    lastTime = millis();
  }

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by adafruit_support_bill »

Your last attempt is part-way there. The next thing is to modify the library so that you can read the sensor in two steps:

Code: Select all

/* Get a new sensor event */ 
  if (millis() - lastTime > sensorReadInterval) 
  {
    sensors_event_t event;
    tsl.FinishReading(&event);  // Read the lux values from the read that was initiated on the previous pass
    luxReading = event.light;
    lastTime = millis();
    tsl.StartNextReading();  // initiate the next read and we will get the results on the next pass.
  }
The older version of the library looks like it might be a little easier to hack. The sensor is read by the getFullLuminosity function below.

Code: Select all

uint32_t TSL2561::getFullLuminosity (void)
{
  if (!_initialized) begin();

  // Enable the device by setting the control bit to 0x03
  enable();

  // Wait x ms for ADC to complete
  switch (_integration)
  {
    case TSL2561_INTEGRATIONTIME_13MS:
      delay(14);
      break;
    case TSL2561_INTEGRATIONTIME_101MS:
      delay(102);
      break;
    default:
      delay(403);
      break;
  }

  uint32_t x;
  x = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
  x <<= 16;
  x |= read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);

  disable();

  return x;
}
You could split this up into two parts:

Code: Select all

void TSL2561::startReading(void)
{
  if (!_initialized) begin();

  // Enable the device by setting the control bit to 0x03
  enable();
}
and:

Code: Select all

uint32_t TSL2561::finishReading(void)
{
  uint32_t x;
  x = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
  x <<= 16;
  x |= read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);

  disable();

  return x;
}

User avatar
terennum
 
Posts: 6
Joined: Sat Apr 09, 2016 3:21 pm

Re: Stepper Motor Significantly Slows Down With Lux Sensor C

Post by terennum »

This is awesome! Thank you. Finally, could you please post the link to the old library? I couldn't find an older version on Github.


Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”