Coin Motor connected to Arduino pin not responding

Hello, I am currently working on project using Arduino Nano 33 IoT and motor. I wrote a code that detects the angle of the Arduino using the embedded IMU Data and rings the motor when the angle is out of range (45 degree in this case). However, when I upload the code to arduino and test it out, it does not work. Here is the code:

#include <Arduino_LSM6DS3.h>

double X, Y, Z;
float gx, gy, gz;
float ax, ay, az;
double Fx, Fy, Fz;
double t, d;
int motorPin = 5;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  while (!Serial);
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
    
  }
}

void loop() {
  unsigned long StartTime = millis();

  if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
    IMU.readAcceleration(gx, gy, gz);
    IMU.readGyroscope(ax, ay, az);
    t = pow(IMU.gyroscopeSampleRate(), -1);
  }

  Z = atan(gz / sqrt(pow(gy, 2) + pow(gx, 2))) * 57.2957795130823;

  if (millis() - d >= 2) {

    Fz = 0.98 * (Fz + t * ay) + 0.02 * Z;
    d = millis();
  }

  Serial.println(Fz);
  
  if (Fz > 45) {

    digitalWrite(motorPin, HIGH);   // turn the LED on (HIGH is the voltage level)
;
  }
  if (Fz <=45){
    digitalWrite(motorPin, LOW);   // turn the LED on (HIGH is the voltage level)
;
  }
}

I’m not sure if this is a hardware issue (current, voltage, etc) or the code issue. I tested out the motor with the 3.3V pin on the Arduino, and the motor works fine. Any advice on this issue?

Thanks!

Arduino output pins cannot be used to power any kind of motor or coil. You may have already destroyed that pin.

Current draw is limited to 20 mA to be safe, and even if the motor draws less than that, an "inductive kick" diode is required to protect the circuit from high voltage transients.

The standard procedure to control a unidirectional motor is to use a bipolar transistor or logic level MOSFET as a switch, as shown below.

Thanks!, I tested if the pin is broken or not by setting multimeter between the output pin and GND pin. I am getting 3.3V between them. Is there still possibility that the pin is broken?

Yes. You can test it by wiring an LED in series with a 220 Ohm resistor from the pin to GND.

Set the output HIGH to light the LED.