We are making a parachute-release for a model rocket. We have an Arduino Nano 33 BLE and a Fitec Micro Servo 9g FS90 as well as various battery options including a HJ 852540 3.7V LiPo (giving 4V) and 4 AA batteries in series.
We are attempting to initiate the servo 4 seconds after the onboard accelerometer detects an acceleration on the x axis of more than 2g. We have so far been able to achieve this (though with the x acceleration set to 0.5g and without the delay to facilitate testing) but as soon as we detach from the USB the system stops working.
We have tested using the layout in the attached image but with power supplied by USB from a laptop, and we're using pin 9 for the PWM signal to the servo. We've tested various battery combinations, including both the LiPo and the 6V from 4 AA batteries in at Vin, and we've also supplied power to the USB socket from a power source (instead of the laptop). In each case the function ceases to work once the laptop connection is removed.
Our code is:
*/
int servoPin=9;
int servoPos=0;
int LEDpin=13;
#include <Arduino_LSM9DS1.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup() {
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
while (!Serial);
Serial.println("Started");
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Acceleration in G's");
Serial.println("X\tY\tZ");
}
void loop() {
float x, y, z;
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
Serial.print(x);
Serial.print('\t');
Serial.print(y);
Serial.print('\t');
Serial.println(z);
if (x > .5) for (servoPos = 5; servoPos <= 90; servoPos += 3) { // goes from 0 degrees to 110 degrees
// in steps of 3 degree
myservo.write(servoPos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
if (x > .5) for (servoPos = 90; servoPos >= 5; servoPos -= 3) { // goes from 90 degrees to 0 degrees
myservo.write(servoPos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position;
}}}
Many thanks for your insights! This is our first Arduino project and we have scoured tutorials and the forum but realise we may be missing material that's already published, so grateful for your patience too!