← Back to list

How to control motors with arduino simulink?

Here’s a practical, Simulink-first recipe you can follow today. It covers DC motors (via H-bridge), servos, and steppers — using only the…

Ampheo · 2025-10-17 07:38 · 0 claps · 2.8 min read
#motor #arduino #simulink #l298n #drv8833
Open on Medium ↗
Wiki topics: 📟 · Gadgets & IoT 🍳 · Food & Cooking

How to control motors with arduino simulink?

Here’s a practical, Simulink-first recipe you can follow today. It covers DC motors (via H-bridge), servos, and steppers — using only the Simulink Support Package for Arduino Hardware blocks.

0) One-time setup (5 minutes)

  1. In MATLAB: Home ▸ Add-Ons ▸ Get Add-Ons ▸ install Simulink Support Package for Arduino Hardware.
  2. Plug your Arduino (Uno/Nano/MEGA/Leonardo/etc.).
  3. In Simulink: Modeling ▸ Model Settings ▸ Hardware Implementation, set Hardware board = Arduino ….
  4. In Solver, choose Fixed-step, discrete (e.g., fixed-step size = 0.01 s).
  5. For live tuning, enable Run on hardware ▸ External mode (Monitor & Tune).

Power rule: Never power the motor from the Arduino 5 V pin. Use a separate motor supply, share GND with the Arduino, and use a driver (L298N, TB6612FNG, DRV8833, A4988, etc.).

1) DC motor with H-bridge (L298N / TB6612FNG)

Wiring (example pins)

  • ENA/PWMA → Arduino D5 (PWM)
  • IN1 → D7, IN2 → D8
  • Motor + supply (e.g., 7–12 V) to driver VIN; common GND with Arduino.

Simulink blocks

  • PWM (Analog Write) block → pin 5 (duty 0–255 or 0–1 depending on version).
  • Digital Output blocks (2×) → pins 7 and 8 for direction.
  • Slider Gain (0–1) or Constant → duty input.
  • Optional: Analog Input A0 for a potentiometer speed knob; Scope for logging.

Direction logic

  • CW: IN1=1, IN2=0
  • CCW: IN1=0, IN2=1
  • Brake: IN1=1, IN2=1 (driver-dependent)
  • Coast: IN1=0, IN2=0

Tip: If you hear audible whine, try a different PWM pin/timer or a driver that supports higher-freq PWM. Keep base rate ≤ 1 kHz unless you know your timing budget.

2) Servo motor (SG90/standard RC servo)

Wiring

  • Servo signal → D9 (or any “Servo”-capable pin), 5 V & GND from an external 5 V source (common GND).

Simulink blocks

  • Standard Servo Write (or Servo Write) → pin 9.
  • Drive it with Step / Sine Wave (mapped 0–180°) or a Slider Gain (0–180).

Notes

  • Many servos draw >500 mA on movement — don’t use Arduino 5 V.
  • If the block expects [0–1], scale angle: Angle/180.

3) Stepper motor (A4988/DRV8825 “STEP/DIR” drivers)

Wiring

  • STEP → D3, DIR → D4, (optional) ENABLE → D2.
  • VMOT (8–24 V typical) to driver; set current limit on the driver; common GND.

Simulink blocks

  • Digital Output → DIR (D4) from a Manual Switch (0/1).
  • Pulse Generator → STEP (D3):
  • Amplitude = 1, Period = 1/fstep, Pulse width = 50%.
  • e.g., 1000 steps/s ⇒ Period = 0.001 s.
  • Optional acceleration: ramp the pulse frequency using a MATLAB Function block that outputs a time-varying period, or switch between multiple Pulse Generators.

Step round-trip

  • For position, add an Encoder if your mechanics allow it; otherwise count generated steps in a Counter Free-Running block for open-loop tracking.

4) Closed-loop speed control (quick DC example)

Add:

  • Analog Input (A0) → read a hall/optical sensor to measure RPM (or use an encoder).
  • Edge Detector + Discrete-Time Integrator (or Counter + Rate Transition) to compute Hz/RPM.
  • PID Controller block → output = duty (to PWM), input = (speed_ref — speed_meas).
  • Run in External mode to tune Kp/Ki/Kd live.

5) Build, run, and tune

  1. Monitor & Tune (External mode) to change sliders/gains live and watch Scope signals.
  2. When happy, Deploy to Hardware for standalone operation (no PC).

6) Troubleshooting checklist

  • Nothing moves: common GND? driver ENA pin asserted? correct pin numbers in blocks?
  • USB resets when motor starts: motor supply back-EMF → add bulk cap near driver (e.g., 470–1000 µF), keep motor power off the Arduino 5 V, ensure diode protection (built-in on most drivers).
  • Jittery servo: weak 5 V rail; try a dedicated 5 V/2 A BEC; keep signal wire short and away from motor leads.
  • Stepper skips: current limit too low, accel too aggressive, or supply voltage too low.

Minimal block “recipes” (copy into a blank model)

DC H-bridge

  • Slider (0–1) → Gain (0–255 if needed) → PWM(D5)
  • Constant(1) → DO(D7), Constant(0) → DO(D8) (swap 1/0 to reverse)

Servo

  • Slider (0–180) → Servo Write(D9)

Stepper

  • Manual Switch(0/1) → DO(DIR=D4)
  • Pulse Generator (Amplitude 1, Period 0.001, Pulse width 50%) → DO(STEP=D3)

메타데이터
post_id
583410dedb32
slug
how-to-control-motors-with-arduino-simulink-583410dedb32
url
https://medium.com/@pqshedy33/how-to-control-motors-with-arduino-simulink-583410dedb32
canonical_url
https://medium.com/@pqshedy33/how-to-control-motors-with-arduino-simulink-583410dedb32
author_url
https://medium.com/@pqshedy33
status
ok
fetched_at
2026-07-16 17:42:09