Window cover and motors on Nano Matter

Hi,
As a lot of people writing here I'm new to Arduino but the Nano Matter got me really interested.
What I want to do use the Window cover and use it to open a door for a chicken coop using a stepper motor so I easily can control it via Apple HomeKit.
I have the sample code for the Nano Matter but and I've tried to add the stepper motor code stings needed but I think there is something that I'm missing.
Does the examples require you to use specific typs of motors and specific pins?

I've tried to go thru the documentation but I just can't seem to find the correct info.

I hope someone here can help me.

/Chris

Hi @cericson_7.

Cool project!

Did you find this sample code on the Internet? If so, please provide a link to it in a reply here on this forum topic.

Or if you found the sample code in Arduino IDE or Arduino Cloud Editor, please provide instructions we can follow to find it there.

Please post your full sketch.

I'll provide instructions you can follow to do that:

  1. If you are using Arduino IDE, Auto Format your code by selecting Tools > Auto Format from the menus.
  2. Click on the window that contains your sketch code.
  3. Press the Ctrl+A keyboard shortcut (Command+A for macOS users).
    This will select all the text.
  4. Press the Ctrl+C keyboard shortcut (Command+C for macOS users).
    This will copy the selected text to the clipboard.
  5. Open a forum reply here by clicking the "Reply" button.
  6. Click the <CODE/> icon on the post composer toolbar.
    This will add the forum's code block markup (```) to your reply to make sure the error messages are correctly formatted.
  7. Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
    This will paste the copied code into the code block.
  8. Move the cursor outside of the code block markup before you add any additional text to your reply.
  9. Repeat the above process if your sketch has multiple tabs.
  10. Click the "Reply" button to post the output.

When your code requires a library that's not included with the Arduino IDE please post a link to where you downloaded that library from, or if you installed it using Library Manager (Sketch > Include Library > Manage Libraries... in Arduino IDE or Libraries > Library Manager in Arduino Cloud Editor) then say so and state the full name of the library.

Well, I was using the example code from Arduino IDE.
Then I added the example code, again, from Arduino IDE for the stepper motors.
You can see it here below.

/*
   Matter window covering example

   The example shows how to create a Matter powered window covering device with the Arduino Matter API.

   The example creates a position aware lift capable Matter window covering device,
   simulates the window covering motor actuation delay and displays the current lift value using the built-in LED.
   The device has to be commissioned to a Matter hub first.

   Compatible boards:
   - Arduino Nano Matter
   - SparkFun Thing Plus MGM240P
   - xG24 Explorer Kit
   - xG24 Dev Kit
   - Seeed Studio XIAO MG24 (Sense)

   Author: Tamas Jozsi (Silicon Labs)
 */
#include <Matter.h>
#include <MatterWindowCovering.h>
#include <Stepper.h>
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

MatterWindowCovering matter_blinds;

void update_onboard_led(uint8_t brightness);

void setup()
{
  Serial.begin(115200);
  Matter.begin();
  matter_blinds.begin();
  pinMode(LED_BUILTIN, OUTPUT);
  update_onboard_led(0);
 
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);

  Serial.println("Matter window covering");

  if (!Matter.isDeviceCommissioned()) {
    Serial.println("Matter device is not commissioned");
    Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
    Serial.printf("Manual pairing code: %s\n", Matter.getManualPairingCode().c_str());
    Serial.printf("QR code URL: %s\n", Matter.getOnboardingQRCodeUrl().c_str());
  }
  while (!Matter.isDeviceCommissioned()) {
    delay(200);
  }

  Serial.println("Waiting for Thread network...");
  while (!Matter.isDeviceThreadConnected()) {
    delay(200);
  }
  Serial.println("Connected to Thread network");

  Serial.println("Waiting for Matter device discovery...");
  while (!matter_blinds.is_online()) {
    delay(200);
  }
  Serial.println("Matter device is now online");
}

void loop()
{
  static uint16_t current_lift_raw = 0u;
  uint16_t requested_lift_raw = matter_blinds.get_requested_lift_position_raw();
  // Return if the requested lift position is the same as the current - no movement requested
  if (current_lift_raw == requested_lift_raw) {
    return;
  }

  // Get the current and requested lift position in percents
  int32_t current_percent = matter_blinds.get_actual_lift_position_percent();
  int32_t requested_percent = matter_blinds.get_requested_lift_position_percent();

  // If the current and requested percentage is the same - then the requested raw difference is so small that it's lost to rounding
  if (current_percent == requested_percent) {
    // Set the actual position to the requested position
    matter_blinds.set_actual_lift_position_raw(requested_lift_raw);
    return;
  }

  Serial.printf("New position requested; current='%d%% (%u)' requested='%d%% (%u)' - ", current_percent, current_lift_raw, requested_percent, requested_lift_raw);

  int32_t step = 0;
  // Determine the direction we need to go to achieve the requested position
  if (current_percent > requested_percent) {
    // Signal the Matter device that we're moving towards the 'closed' position
    matter_blinds.set_current_operation(MatterWindowCovering::WINDOW_COVERING_CLOSING);
    Serial.println("closing...");
    step = -1;
    
        // step one revolution  in one direction:
    Serial.println("clockwise");
    myStepper.step(stepsPerRevolution);
    delay(500);


  } else {
    // Signal the Matter device that we're moving towards the 'open' position
    matter_blinds.set_current_operation(MatterWindowCovering::WINDOW_COVERING_OPENING);
    Serial.println("opening...");
    step = 1;

    // step one revolution in the other direction:
    Serial.println("counterclockwise");
    myStepper.step(-stepsPerRevolution);
    delay(500);

  }

  // Iterate while we're not in the requested position
  do {
    // Step one percent in the requested direction
    current_percent += step;
    // Calculate the LED value and display it
    uint8_t current_led_brightness = map(current_percent, 0, 100, 0, 255);
    update_onboard_led(current_led_brightness);
    // Print the current percent
    Serial.printf("%d%%\n", current_percent);
    // Delay a small amount to simulate a real motor actuating
    delay(50);

    // Update the Matter network with the current position
    matter_blinds.set_actual_lift_position_percent(current_percent);
  } while (current_percent != requested_percent);

  // Set the actual position to the requested position
  // Percent calculation rounds down to the nearest integer - this smooths out all rounding losses if a fraction of a percent is requested
  // and sets the actual position exactly to the requested
  matter_blinds.set_actual_lift_position_raw(requested_lift_raw);
  // Signal the Matter device that we're not moving anymore
  matter_blinds.set_current_operation(MatterWindowCovering::WINDOW_COVERING_STOPPED);
  // Print the current position
  Serial.printf("Moved to %u%%\n", matter_blinds.get_actual_lift_position_percent());
  current_lift_raw = requested_lift_raw;
}

void update_onboard_led(uint8_t brightness)
{
  // If our built-in LED is active LOW, we need to invert the brightness value
  if (LED_BUILTIN_ACTIVE == LOW) {
    analogWrite(LED_BUILTIN, 255 - brightness);
  } else {
    analogWrite(LED_BUILTIN, brightness);
  }
}

Again, I'm a beginner on this but it looks ok to me to do it this way :smiley:

Thank you for helping out!

/Christofer

Thanks for the clarification.

You can use the example with any unipolar, bipolar, or five phase stepper motor. The "Stepper" library is for use with basic stepper drive circuitry. If you are using a modern stepper driver like the A4988 or DRV8825 that has a step and dir control interface, then the "Stepper" library will not be appropriate and you should use a different library instead. If you aren't sure, you can provide information about the specific motor and drive hardware you are using and we'll provide further assistance.


You can learn more about the Stepper library from the documentation here:

https://docs.arduino.cc/libraries/stepper/

My recommendation is to start by using the simple "stepper_oneRevolution" example sketch to test the functionality of your stepper motor. You can open the example by selecting File > Examples > Stepper > stepper_oneRevolution from the Arduino IDE menus. After you upload that sketch to your Nano Matter board, you should see the stepper motor alternatively rotating clockwise and counterclockwise.

Yes, the pins for controlling the stepper motor are defined here:

See the Stepper motor documentation to understand the specific connections for these pins. If you want to use different pins, just change that line of the sketch.

Thanks for you reply!

So I’m using, or was planning to use at least, the 28BYJ-48 stepper motor and the ULN2003 driver board that I’m feeding that with an extra power supply so it has 5v.

Should I change to one of the driver boards that you sugessted to make it work?

And I am using the stepper_oneRevolution example in the code :smiley:

That hardware will work fine with the example sketch.

It is definitely not necessary, and would even cause you some difficulty since you would need to modify the example sketch code.

Those stepper drivers are superior in several ways. They only require two pins on the Arduino board instead of four, which would be important in a project where you have so many things connected to the board that you are running out of pins (but isn't important in projects where you have plenty of pins to spare). They also reduce the amount of processing power is needed to control the stepper motor since you only need to pulse a single pin in order to rotate the motor instead of four pins (but the Nano Matter has a lot of processing power so this isn't important for a project where there are plenty of extra CPU cycles to spare).

Is the stepper motor moving as expected?

Good the hear that I can use the stepper and controller that I have.

No, it's not moving...

Strange....

Protocol stack is set to Matter
Programmer is set to OpenOCD

I know the stepper is working because I used the example Stepper code on a "normal", or should I say non Matter, Nano the stepper motor runs just fine in a loop but it just fine.

I think the next step would be to try that same simple example code on your nano matter.

I tried that and it won't move the stepper motor then either, unfortunately...
I have two Nano Matters and they both act in the same way.

Any one who has the hardware and can try it? :blush: