I am using the MU Vision sensor which is supposed to drive a stepper motor. The MU Vision sensor code works. The moment I turn on the power for the stepper motor power supply, the code stops. I have disconnected all the wires connecting the stepper motor to the Arduino.... It still halts the code.

To be honest I am not 100% sure what the fps value is for on the MU Sensor, but when it's working the value stays between 18 and 20. The moment I switch on the power to the powersupply this value jumps to 500 or 1000 and it halts.

I am using an original Arduino Mega, powered via USB from my PC. There is no link between the stepper motor and the Arduino. Below is a sample of my code, but I somehow don't think it is a code fault, I am thinking it is some kind of electrical interference maybe?

Some additional info :

I am using the wiring and settings as given on : https://wiki.dfrobot.com/MU_Vision_Sensor_SKU:%20SEN0314#target_3

  1. The Mu Vision Sensor works by itself (using their example code) - so wiring is correct

  2. I have NO additional wires connected to the Arduino, only the USB cable from the PC

  3. The last couple of lines of code prints "fps = " and a value.... When it runs smoothly it is always fps=18 (to about 20) - when the power supply is switched on it jumps to fps=500.... fps=1000 and then halts. So it's almost as if there is electrical interference with the internal clock.

  4. Again... the stepper runs totally independent of the Arduino in terms of power supply and for this test I have no wires connected to the Arduino and all stepper motor code has been removed.

  5. I am going to test now on an ESP32 and see if it is still the same. I have already swopped from an Arduino Mega to UNO and still have the same result (Did this and same thing happens with ESP32 - except ESP32 basically reboots itself and tries again, so the code keeps running)

  6. Lastly... I did add a blinking LED too see if that would possibly carry on working when the code stops, but the LED stops blinking too.

  @brief MU Vision Sensor

   @copyright    [DFRobot](https://www.dfrobot.com), 2016
   @copyright    GNU Lesser General Public License

  @author [WWZ](Wenzheng.wang@dfrobot.com)
  @version  V1.0
  @date  2019-03-11

  GNU Lesser General Public License.
  All above must be included in any redistribution
* ***************************************************

#include <Wire.h>

#define MU_ADDRESS    0x60
#define PROTOCOL_VER  0x03
#define VISION_ID     0x05      // body

// register define
#define REG_PROTOCOL_VER  0x01
#define REG_LED1_CONF     0x06
#define REG_LED2_CONF     0x07
#define REG_LED_LEVEL     0x08
#define REG_CAMERA_CONF1  0x10
#define REG_FRAME_CNT     0x1F
#define REG_VISION_ID     0x20
#define REG_VISION_CONF1  0x21
#define REG_PARAM_VALUE5  0x29
#define RESULT_NUM        0x34
#define RESULT_DATA1      0x40
#define RESULT_DATA2      0x41
#define RESULT_DATA3      0x42
#define RESULT_DATA4      0x43
#define RESULT_DATA5      0x44
// color
#define MU_COLOR_BLACK                0x01U
#define MU_COLOR_WHITE                0x02U
#define MU_COLOR_RED                  0x03U
#define MU_COLOR_YELLOW               0x04U
#define MU_COLOR_GREEN                0x05U
#define MU_COLOR_CYAN                 0x06U
#define MU_COLOR_BLUE                 0x07U
#define MU_COLOR_PURPLE               0x08U

// Define Stepper pins

int reverseSwitch = 2;  // Push button for reverse
int driverDIR = 6;    // DIR+ pin
int driverPUL = 7;    // PUL+ pin

// Variables

int pd = 800;       // Pulse Delay period
boolean setdir = HIGH; // Set Initial Direction
int stepsTaken = 0;

int i2c_read8(uint8_t reg) {

  Wire.requestFrom(MU_ADDRESS, 1);
  return Wire.read();
void i2c_write8(const uint8_t reg, const uint8_t value) {

uint8_t reg[][2] = {
  { REG_VISION_ID,      VISION_ID }, // set vision type = vision_detect
  { REG_VISION_CONF1,   0x21 }, // vision begin
uint8_t frame_count_last = 0;

void setup() {
  // put your setup code here, to run once:

  //pinMode (driverPUL, OUTPUT);
  //pinMode (driverDIR, OUTPUT);
  //attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);


  if (i2c_read8(REG_PROTOCOL_VER) == PROTOCOL_VER) {
    Serial.println("device initialized.");
  } else {
    Serial.println("fail to initialize device! Please check protocol version.");
  for (uint32_t i = 0; i < sizeof(reg) / 2; ++i) {
    i2c_write8(reg[i][0], reg[i][1]);

void loop() {
  // put your main code here, to run repeatedly:
  long time_start = millis();
  int frame_count = 0;
  // waiting for update
  do {
    frame_count = i2c_read8(REG_FRAME_CNT);
  } while (frame_count == frame_count_last);
  frame_count_last = frame_count;

  i2c_write8(REG_VISION_ID, VISION_ID);
  // read result
  if (i2c_read8(RESULT_NUM) > 0) {
    Serial.println("body detected:");
    Serial.print("y = ");
  } else {
    Serial.println("body undetected.");
  Serial.print("fps = ");
  Serial.println(1000 / (millis() - time_start));
  • "the code stops" Stops where? – timemage Feb 22 at 17:47
  • 2
    Please add a schematic and/or picture of the wiring. If you are correct that it is an electrical problem rather than code, the code by itself isn't enough for folks to help you. // I might move the time_start to global & update it with frame count & the fps calc before the i2c_write so it's independent of the loop() // Maybe the power draw is making the sensor stop sending frames? – Dave X Feb 22 at 18:15
  • 2
    usual suspect is I2C... That is the only thing without any timeouts in it and it's quite susceptible to deadlocks – KIIV Feb 22 at 18:46
  • 1
    did you disconnect the ground and vcc wires? I bet your stepper draws too much current and drops the MCU voltage, even just briefly, to the point it fails. – dandavis Feb 22 at 21:22
  • 1
    @Dave X - To be very honest, I am not sure what the whole fps calc is for. IS it even needed? This is part of the MU Vision Sensor sample code and the documentation isn't great. – Heinrich Helmbold Feb 24 at 16:15

This does not resolve the issue, but for now it is "fixed". On the I2C interface stepper driver is interfering with the microcontroller, and it must be some electrical interference, if I disconnect the stepper motor from the stepper driver then it works. (Just saying again... NOTHING is plugged into the Arduino from the stepper driver or the stepper motor - so this is purely electrical / magnetic interference)

I managed to get the MU sensor working on UART instead of I2C and it seems like there is no interference here and it keeps running.... fingers crossed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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