Okay I'm stuck. I'm using a Qt Py Mo (SAMD21), an LIS3DH and an SG90 servo. I want the Y axis (or whatever axis) on my LIS3DH to drive my servo. I have my accelerometer connected via stemma, I'm using a breadboard to connect my servo to pin 9 and I have the power and ground on the servo connected to the QT PY using 5v and ground on the breadboard. My code doesn't seem to be doing anything! I'm getting nothing. Anyone care to take a look at my code?
#include <Wire.h>
#include <Adafruit_LIS3DH.h>
#include <Servo.h>
// Set up the LIS3DH accelerometer
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
// Set up the servo
Servo servo;
void setup() {
Serial.begin(9600);
// Initialize the LIS3DH accelerometer
if (!lis.begin(0x19)) {
Serial.println("Could not initialize LIS3DH");
while (1);
}
// Initialize the servo
servo.attach(9);
}
void loop() {
// Read the accelerometer data
lis.read();
// Get the Y-axis acceleration value
int yAccel = lis.y;
// Map the Y-axis acceleration value to a servo angle
int yAngle = map(yAccel, -1000, 1000, 0, 180);
// Set the servo angle
servo.write(yAngle);
// Delay for a short time to allow the servo to move
delay(10);
}
Hey Railroader sorry I’m new to this and I’m seeing you asked me to post schematics. I should have replied to your message but instead replied to my original message. Let me know if you don’t see the schematics I posted and I’ll respond again to your message. Sorry!
If You had rotated the picture 1/4 of a rev, counter clockwise it had been nice. My wall mounted monitor is not rotatable... Joking! Not easy to read and understand the texted parts.
One issue is: No microcontroller is a power supply for any motor. Give the motor its own power supply.
Next issue is: There's only one wire named "Stemma connection". If it's only one cable nothing can work.
Hey I really appreciate the feedback! I don’t know how to give kudos or whatever on this forum. I’ll take a look online and see if there’s a way to give kudos.
I’ve attached a rotated picture which I will make note of moving forward.
The stemma is the highlighted red cable in the second attached pic. I believe this transmits data, power and has ground.
Pictures are not the way to show connections. A criminal detective have no clues might do it but not engineers.
Your post gives the impression that nothing is happening. Very often the blame is improper wiring. Therefore ever wire, or missing wire, could be the reason.
void loop() {
// Read the accelerometer data
lis.read();
Add a Serial.print and S'serial monitor for the data received at this read.
Add a Serial.print of the yAccel received.
Add a Serial.print of the yAngle.
Okay you were correct Railroader. The accelerometer is generating values so I’m good there. Again, I’m really new to this so I apologize for the stupid questions. I’ve been poking at this for weeks and I should have thought about that.
Good c all on the servo power. I’ll try a separate power supply. Thank you and I’ll report back!