One is Servo and Laser, YoshiTrainer 2000!

Today we are dealing with a very serious issue: the fitness of our dog Yoshi.

Since we have been in our shop, Delicious for YoshiYoshi's calorie intake has increased disproportionately.

Actually, we wanted to build an automated treat dispenser for Yoshi soon, who will automatically hand out the donated Leckelis, but that would be irresponsible if we did not provide for the fitness of the dog. 

So we need to think about a sports program for Yoshi.  And then came the idea with the YoshiTrainer 2000!


  • I have all the necessary components on my desk
  • Programming is relatively simple as we completely ignore my last blog post about random numbers and use random()
  • The device is Y2K compatible


For the project we need:

1x AZ-Delivery microcontroller, compatible with Arduino Uno R3

2x Servo motor e.g. AZ-Delivery Servo MG996R

1x Laser Diode

1x Mini-Breadboard

1x Jumper Cable Set

1x 5V power supply with sufficient power for servos, laser & Uno

As well as some rolls of VDE insulating tape and double-sided tape.


Laser warning:

The laser diode used is a Class 1 laser. According to DIN EN 60825-1, this means: "The accessible laser radiation is harmless, or the laser is in a closed housing".

Nevertheless, I recommend the use of goggles. In addition, lasers require sufficient cooling in continuous use. 

I only have one MG 996R servo, so I took our SG90 for the Y axis. This is much smaller, has gears made of plastic and has much less power.

We wire the whole thing as follows:


5V 10A Power Supply






Microcontroller, compatible with Arduino Uno R3






Microcontroller, compatible with Arduino Uno R3



Servo 1 Orange

PWM 10

Servo 2 Orange


Laser S

Servo 1 and 2 Red

Breadboard +

Servo 1 and 2 Brown

Breadboard -

Laser -

Breadboard -


Here is a photo where you can see the wiring:



Alternatively, here the same as schematic:




Now the programming is still missing. 

I use the servo.h supplied with the IDE

With "Servo servoX;" and "Servo servoY;" I create 2 servos named ServoX and servoY. 

In the setup I then define on which pin the control line of the servo hangs, in our case e.g. "servoX.attach(9);" for servoX.

Now we can read or set the position of the servo with the functions and servoX.write().

So that Yoshi has a chance to catch the point, I've packed it all in a while loop, with small delays. The variable "spd" can be used to roughly push up the speed.

For the laser, we first use the pin as an output using "pinMode(laserPin, OUTPUT);" and then switch it on with "analogWrite(laserPin, 40);". 


Here is the complete source code:


#include <Servo.H>
Servo servoX;
Servo servoY;

uint8_t tPosX = Random(0,180); Target position Servo X
uint8_t cPosX = servoX.Read(); Current position Servo X
uint8_t tPosY = Random(0,180); Target position Servo Y
uint8_t cPosY = servoY.Read(); Target position Servo Y
uint8_t Spd = 1; Speed
Int laserPin = 3; PWM Pin for laser diode 

Void Setup() {   put your setup code here, to run once:   servoX.Attach(9); Servo X to Pin 9    servoY.Attach(10); Servo Y to Pin 10   pinMode(laserPin, Output);   analogWrite(laserPin, 40); brightness laser - observe heat development! 

Void Loop() {   tPosX = Random(45,135); this allows the range between 0 and 180° to be   cPosX = servoX.Read(); Read current position   tPosY = Random(120,170);   cPosY = servoY.Read();   while (tPosX > cPosX){     cPosX += Spd;     servoX.Write(cPosX);     Delay(50);     cPosX = servoX.Read();   }   while (tPosX < cPosX){     cPosX -= Spd;     servoX.Write(cPosX);     Delay(50);     cPosX = servoX.Read();   }   while (tPosY < cPosY){     cPosY -= Spd;     servoY.Write(cPosY);     Delay(50);     cPosX = servoY.Read();   }   while (tPosY > cPosY){     cPosY += Spd;     servoY.Write(cPosY);     Delay(50);     cPosX = servoY.Read();   }
Delay(500); So that Yoshi doesn't get too tired

And we have a partial success. A laser point that travels more or less randomly through the area.

Just like that - or at least similar - the first laser shows must have looked like that at that time.


We hope you liked today's blog post. Please write a short comment if you would like more factual contributions or if you liked today's post.


For arduinoProjects for beginners

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN