In today's blog post, we control a AZ-Delivery Servo MG996Rvia a Joystick module.
To do this, we need:
1x AZ-Delivery microcontroller, compatible with Arduino UNO R3
1x Set of 3 40 pcs. Jumper Wire m2m/f2m/f2f
The servo pulls up to 2.5A in operation - far too much for our microcontroller, compatible with Arduino Uno R3. Therefore, we need to supply it with an external power supply.
We build up the circuit below. It should be noted that we connect GND from the Arduino to the GND of the external power supply.
There are 3 cables on the servo. The darkest (in our case brown) is GND. Red is like almost always VCC (4.8 - 7.2 V current consumption: up to 2.5A), and orange is controlled by the microcontroller, compatible with Arduino Uno R3 and connected to pin 9.
The joystick has 5 connection pins which are connected to the microcontroller, compatible with Arduino Uno R3, as follows:
Joystick |
Microcontroller, compatible with Arduino Uno R3 |
Gnd |
Gnd |
+5V |
5v |
VRx |
A0 |
Vry |
A1 |
Sw |
7 |
For programming we take the library with the simple name "Servo".
We also only use the X axis to rotate the servo in the desired direction.
#include <Servo.H>; Servo servo1; Give the servo a name #define joyX A0 Pin A0 - Joystick X #define joyY A1 Pin A1 - Joystick Y #define joyBTN D7 Pin D7 - Joystick Button Int Item = 0, You = 0, xValue, yValue; Variables for position (pos), direction (dir), and the X and Y value of the joystick Void Setup() { Serial.Begin(9600); servo1.Attach (9); Control line from the servo is connected to pin 9 } Void Loop() { xValue = analogRead(joyX); Query the X position of the joystick yValue = analogRead(joyY); Query the Y-position of the joystick Output to Serial Monitor - only for troubleshooting Serial.Print(Item); Serial.Print("Atin"); Serial.Print(xValue); Serial.Print("Atin"); If (xValue < 490){ If Joy after links <- Item = --Item; pos gets energized } If (xValue > 500){ If Joy is right Item = ++Item; pos we increased } If (Item < 0){ So that pos does not become negative Item = 0; } If (Item > 180){ So pos doesn't go over 180 Item = 180; } Delay(20); servo1.Write(Item); Move servo to position "pos" }
After uploading the code, we can now rotate the servo in the desired direction using the joystick.
We hope that our blog post today has inspired you and we look forward to your comments. Until the next post from AZ-Delivery, your expert in microelectronics!
2 comments
Andreas Wolter
@Robert Miosga:
damit das Servo stehen bleibt, würde ich ein Maximum einbauen. eine Variable, die immer dann überspeichert wird, wenn der neue Wert größer ist, als der alte. Diesen Wert dann auf das Servo geben, falls er sich geändert hat.
Die Geschwindigkeit, mit der sich die Servoachse dreht, wird in diesem Fall wahrscheinlich mit der Zeile delay(20) festgelegt.
Was Sie brauchen ist die Beschleunigung. Je größer die Beschleunigung, desto kleiner der Wert in der delay() Funktion. Die Beschleunigung ist die erste Ableitung der Geschwindigkeit. Die Geschwindigkeit zu messen, geht über die Zeit. Das ist der mathematische Ansatz dahinter. Dafür gibt es viele Beispiele im Netz. Sie müssen es dann “nur” noch in Code umsetzen.
Ohne Mathe würde ich immer den gleichen Zeitraum messen und dann gucken, um wieviel sich der Wert erhöht hat. Den Abstand der beiden Werte innerhalb der Zeitmessung könnte man dann ins Verhältnis zum Wert in der delay() Funktion setzen. Also bräuchte man dafür eine minimale und maximale Drehgeschwindigkeit der Servoachse.
Ich hoffe, ich konnte ein wenig weiterhelfen.
Andreas Wolter
Robert Miosga
Hallo, ich habe eine Frage, ich versuche schon seit Langen Servos mit einem Joystick zu steuern, aber mein Ziel ist es das der Servo sich in die jeweilige Richtung bewegt und dann auch dort stehen bleibt .. und sich nicht wieder zurückbewegt nachdem der Joystick sich zentriert .. zusätzlich soll der Servo sich proportional schneller bewegen je nach dem wie sehr man den Joystick in die jeweilige Richtung drückt .. können Sie mir eventuell weiterhelfen oder mir Ansätze geben wie man das angeht ?