More Raspberry Pico material – Servos

Several years ago, HackSpace magazine put out a book on the Raspberry Pico controller. I’ve been doing several projects for our local model railroad club using the Pico. I have also created some training for the membership to get them up-to-speed on some common interfaces. The next few posts will be about the chapters I have written to extend the material in the Getting started with MicroPython on the Raspberry Pi Pico book. If there are any overlaps in the material covered (either from that book or from some of the previous posts on this site) they have been updated and Americanized. I plan to put out each of the extension chapters and appendices, so others can review them without downloading the entire book I put together for the class. The cod e in these examples are written in MicroPython.

This focuses material on the 9G servo, but the concepts are the same with others. This servo can rotate approximately 180 degrees (90 in each direction). The ‘9G’ means that the servo weighs 9 grams.

What is a servo motor? 

A popular hobby market motor, the servo motor can push or rotate an object with great precision and is thus quite useful for projects that require precise control of angular or linear position, velocity, and acceleration. If you need to rotate and object at some specific angles or distance, then you use a servo motor, which in turn is made up of a simple motor running through the servo mechanism. The servo mechanism comprises three parts; first is the controlled device, second is the output sensor and lastly, the feedback system. In this closed loop system, a positive feedback system is used to control motion and the final position of the shaft. The device is controlled by a feedback signal produced by comparing output signal and reference input signal.

The model of your servo is:

Figure 11-1 – Servo wiring diagram

Where the voltage is carried on the red wire, the ground on the brown wire and the PWM (pulse-width modulation) signal is on the orange. Other sizes and brands of servos will have other wiring colors and voltages. A good in-depth overview of PWM is located at: Generating PWM Signals Using the Raspberry Pi Pico another good article is The Pico In MicroPython: PWM (i-programmer.info)

Using a servo motor with a Raspberry Pi Pico 

First, we need to wire up the servo as shown in the following illustration:

Figure 11-2 – Servo breadboard diagram

Now we need a short program that will run the servo through its range:

#imports
from machine import Pin,PWM
import utime
#constants
MIN = 1000000
MAX = 3000000
NumberOfSteps = 20
SERVO_GPx=15
#variables
increment = int((MAX-MIN)/NumberOfSteps)
pwm = PWM(Pin(SERVO_GPx))
pwm.freq(50)
#main loop
while True:
    x = range(0,NumberOfSteps)
    movement = MIN
    pwm.duty_ns(MIN)
    utime.sleep(1)
    for n in x:
        movement = movement + increment
        print("movement ",movement)
        pwm.duty_ns(movement)
        utime.sleep(0.02)

You’ll notice there are two constants indicating the minimum (MIN) position, the maximum (MAX) position and the mid position of the servo. We have another constant for the number of steps to divide the range of motion. We then create a pin configured as a PWM and set its initial frequency to 50. 
Then in a while True loop, we set its initial position to min. Next, we wait for a second, and increment through the range. There is a pause of 0.02 seconds since it takes a little bit of time for the servo to respond. 

Save the program as 11-1 Servo.py and run it.

Lesson challenge – Change the number of positions to a different number and watch what happens. What if you only wanted to move 45 degrees, how would you do that? How about trigger the servo based on a switch being pressed? How could you make a clock?

Using a servo motor and limit switches

Although servos generally rotate the same amount each time, it may be necessary to install a limit switch in your model. This will tell the Pico that its rotation has taken place as far as the servo should go and to stop the rotation. This next program will take rotate the servo until a limit switch is triggered, telling your program to stop the rotation.

In this case, I added a limit switch on the end of the breadboard and held the servo so it would touch the switch before it reached its limit. Remember that since we are using a pull down, we need to connect the switch between the power bus and the pin 14.

Here is the code:

#imports
from machine import Pin,PWM
import utime
#constants
MIN = 1000000
MAX = 3000000
NumberOfSteps = 20
SERVO_GPx=15
LIMIT_GPx=14
#variables
pressed = False
limitSwitch = machine.Pin(LIMIT_GPx, machine.Pin.IN, machine.Pin.PULL_DOWN)
increment = int((MAX-MIN)/NumberOfSteps)
pwm = PWM(Pin(SERVO_GPx))
pwm.freq(50)
# interrupt handler
def limit_handler(pin):
    global pressed
    if not pressed:
        pressed=True
        print("limit reached")
#main loop
while True:
    x = range(0,NumberOfSteps)
    movement = MIN
    pwm.duty_ns(MIN)
    utime.sleep(1) # let the physical system catch up
    pressed = False
    limitSwitch.irq(trigger=machine.Pin.IRQ_RISING, handler=limit_handler)
    for n in x:
        movement = movement + increment
        print("movement ",movement)
        pwm.duty_ns(movement)
        utime.sleep(0.2)
        if (pressed):
            break

The actual wiring understanding is an exercise for the student.

Lesson challenge – How would you make your servo run based on pressing a button? After all, that is an example what you would do with any animated display. How about a servo with variable resistors to control the lower and upper limit of movement?

Finally looking into Tailscale

I have had a home virtual private network (VPN) for nearly a decade. I’ve used it to access my home environment from all over the world. It has been relatively low maintenance, but when there is something shiny and new(ish) I want to investigate it.

Recently, I heard some very positive things about Tailscale, so I thought I’d give it a chance. Tailscale is a partially open-source, software-defined mesh (peer to peer) VPN service. The company provides a zero config “VPN as a service”. It is free for personal use. I could always set up my VPN again if they change that.

I’ve found Tailscale to provide nearly seamless access to my Linux, Android and Windows machines. Right now, I use it to monitor my 3D printing when I am out of the house since I can access the Web server within my 3D printer remotely. I could run my ham gear remotely as well.

It literally took minutes to setup on each device. The hardest thing for me to wrap my mind around was how easy it was to use. Each device gets its own VPN defined IP address that you can ‘just use’ like it is on your device’s network (no matter what network it is currently connected up with) – mind blown 🤯.

I can even show you a picture of my configuration since you’ll not be able to access it.

I have a headless Raspberry Pi sitting on my network that provides local access to my home devices, much like I had a headless Pi working as my VPN.

There are many videos and overview documents out there if you want to know more. I believe it is a game changer.

PETG bed adhesion

I may have mentioned before that PETG is my filament of choice when 3D printing. One issue that people run into with PETG is that it often sticks too well to the print bed and destroys the bed surface more quickly than other filament types. One way to get around this is using glue stick. In fact, my latest Creality K1 came with a glue stick and the bed even says right on it that glue stick needs to be applied.

Many people recommend (though there is some controversy) applying glue stick or some other release agent to the build surface for printing PETG. It’s a fact that PETG can adhere too firmly to almost all types of build surfaces. PEI is generally seen as the best surface to use for PETG, but even it can sometimes have its surface pulled free. I have had good luck with this surface, with and without glue stick.

While intuition would tell us that the glue stick would make the PETG stick better to the build surface, the actual purpose of the glue stick is quite different.

When the build surface is clean enough for PETG to stick to it firmly, the purpose of the glue stick is to act as a release agent, preventing the PETG from adhering to the surface so well that it becomes destructive to remove. A water-soluble glue stick provides a new surface for the print to adhere to rather than directly on the build plate. The glue can be wiped off periodically with a damp cloth and the surface should be clean and dry and glue reapplied before the next print, if you start to have adherence issues again.

One advantage that the glue surface provides is that it is water soluble, so if you are having trouble getting the print off the plate, you don’t need to resort to the scraper or a straight razor as the first tool you reach for – you can use water to dissolve the glue and break the part free.

Which glue stick

Elmer’s glue stick is recommended: it’s cheap, it gets the job done… The Disappearing Purple glue stick is a nice choice, if you don’t want to invest in specialty adhesives right off the bat. It’s purple, so you can tell where you’ve spread it (importantly for bed adhesion, you can see how evenly you’ve spread it) then dries clear. Try it, validate that it works. You can always try something else.

Using the glue stick

Assuming you’ve already made your print bed preparations, (cleaning, leveling, and adjusting) here are the steps to follow:

  1. Use a heated printed bed as you normally would.
  2. Spread a thin layer of glue either to the area that will be taken up by the print or to the entire print bed.
  3. Wash the glue off with warm water when done printing. It is glue, so it will attract dust… over time.

You do not have to reapply glue every time you print. You can print between 5 to 10 prints without having to reapply glue for a new print. 

Still having trouble with bad adhesion

First make sure the bed is clean and there are no fingerprints. PEI and fingerprints, means no adhesion.

If you are having an issue with bed adhesion on smaller parts, a raft or brim should be used.

A brim provides lines of filament surrounding the print to increase the contact area to the build plate. This is the first choice I use in most cases.

raft provides a platform that’s printed onto the build plate, and the part is printed on top of the raft. This also increases surface area with the build plate and allows you to easily remove the initial layers from your model.

Each technique has its own pros and cons (besides increased time to print).

Installing a new hot end in the Creality K1 (Max)

In my ongoing battle to become familiar with the K1 series, I thought I’d write up how to replace the hot end (since I had one really clog on me the other day) – that is why I wrote up the post on loading and unloading filament, that some people (on the Internet) thought was gross overkill.

There is a service tube video of hotend replacement, but more explanation is likely necessary for some, so this is my attempt. The installation process starts about one minute into the video.

First, it would be good to show what comes with a hotend kit. The hotend (obviously), as well as a bag of screws to attach it to the printer.

Hot end in the silicon sleeve (which needs to be carefully removed to install)

Breakdown of the hotend with the sleeve removed

Now the process to replace it.

1) Make sure the printer is turned off

2) Add appropriate thermal grease to the upper end of the throat tube and insert the throat tube into the mounting hole of the heat sink. Some people say this is not necessary, but it can’t do any harm.

3) Insert the connectors of the heating tube wire and thermistor into the corresponding terminal ports on the sub-board. We are doing this now since it may be too tight to get the connectors in after the hot end is installed. Make sure the little alignment marks on the plugs are pointed down to fit in the slots the sockets.

4) Turn the bump on the bottom of the brass heating tube towards the front of the machine, then secure the hotend by tightening the (long) hotend fixing screws and the upper screw of the throat tube. During the tightening process, ensure that the hotend is installed vertically, and that the mounting screws on both sides of the hotend bracket are equal in force. There should be a set of these screws included with your hot end kit.

5) If you need to replace the nozzle (you shouldn’t need to do this with a new hot end), use the open end wrench and the socket wrench provided when you purchased the printer. Place the open end wrench on the hot end brass mount (to use as the opposing wrench). Place the nozzle in the socket end and tighten it into place. Do not over tighten. If you use a torque wrench instead of the socket provided you are only looking for about 2.5 – 3 Nm (Newton Meter).

Note: You should finger-tighten the hotend cold, but final tightening should be done hot (240C), to ensure a tight fit.

6) Turn on the printer and set the temp to 240C. Expect some smoke.

7) Tighten the two hotend fixing screws and the hotend set screw at the back of the heat sink, to ensure the hot end is secure.

8) Load filament

9) Check to ensure that the filament extruded properly during the ‘extrude’ process (or if you just push it through while the hotend is on).

10) Turn the hot end temp off and turn on the fans to let the device cool. Once cool enough to touch turn the printer off.

11) Align the silicone sleeve with the nozzle and slide it over the hotend, ensuring that the silicone sleeve completely covers the hotend and the nozzle is surrounded by the sleeve. Be careful of the wires and the thermister sensor, since they can be easily detached.

12) Reassemble the extruder fan cover – Pick up the fan cover, insert the model cooling fan connector into the corresponding terminal port on the front of the nozzle sub-board. Slip the cover over the two knobs at the top of the assembly.

13) Fasten the fan cover and tighten the two screws on the left and right sides of the fan cover.

14) Reassemble the AI Lidar (unnecessary on the K1) – Insert the connectors for the AI Lidar connection cable into the corresponding mounting port on the nozzle sub-board, then insert the alignment posts at the top of the module into the alignment holes on the mounting bracket and secure them with the two screws.

15) Turn the printer back on.

16) Recalibrate the printer. You should do this whenever you mess with the extruder – in my opinion.

I am sure I left something off, so I’ll update it the next time I need to replace a hotend.

Bard’s Art Generation

A while back I posted about playing with some AI art generation. I followed up with a post using Leonardo.ai. Since Bard sent me a note telling me they generate art, I thought I’d try again.

Prompt: “An impressionist painting focused on moving beyond the intersection of technology and life, generating value at the crossroads of life and technology”

Prompt: “Create an oil pastel focused on the concept of moving beyond the intersection of technology and life, generating value at the crossroads of life and technology”

Prompt: Create a cubist image focused on the concept of moving beyond the intersection of technology and life, generating value at the crossroads of life and technology

Prompt: Create a pixel art image focused on the concept of moving beyond the intersection of technology and life, generating value at the crossroads of life and technology

I find these images less interesting (though more colorful) than the previous AI generated art attempts (and strangely many of those generated [not shown] depicted various minorities and individuals with disabilities) – not sure what that is about, unless someone at Google thinks that art is not diverse enough.

Creality K1 extruder loading and unloading checklist

This printer has some features that are supposed to help you load and unload filament. The load and unload capabilities work well – when executed in a very specific way. If you just wing it, there is a high likelihood that you will clog the extruder. I didn’t believe this until it happened to me twice in one week. Here is my checklist for unloading and loading filament:

Unloading filament (these steps may seem like overkill, but it seems to be necessary, since you can clog the printer during the filament unload process very easily):

1) Turn on the printer and wait for the user interface to quiesce (settle down).

2) Go to the filament load and unload screen

3) Click on Retract and wait for the retraction process to Finish. Make sure the temperature is appropriate for the filament being used. During the process, the printer will extruder a small amount of the filament to ensure the hot end is up to temp and then retract the filament out of the hot end/extruder.

4) Unlock the filament lock on top of the machine. The picture shows the lock in the locked position. If you don’t do this, there is a high likelihood that you will leave some unwanted filament in the machine.

5) Take the filament Bowden tube (the white tube in the picture) off the printer. To do this remove the blue clip, press down on the black ring and pull up on the tube. I really hate to do this, but it seems necessary, based on what you will see when you pull it out — not the orange blur, which is a filament artifact dangling on the end.

6) Check for any strange filament extrusion artifacts and remove them. If you do not do this, the little blob of filament can lodge in the tube, the extruder, or the filament sensor! This is the voice of experience speaking, since I have had all three clogs take place.

7) Roll the filament back on the reel and remove the filament spool. Clip the filament in place so it does not tangle.

Loading filament

1) Cut the end of the filament at an angle (at least 45 degrees) and ensure that it is not tangled. Place the spool on the printer, so that it will unroll in alignment with the insertion tube.

2) Insert the filament into the tube at the back of the machine. Ensure that the filament doesn’t come off the spool in the process.

3) Push the filament through the tube until you see it reach the end of the tube above the extruder.

4) Make sure the lock filament lock is in the unlocked position.

5) Push the filament into the extruder. We’re talking about ¾”, at least. If you face resistance, wiggle the filament back and forth. If you cannot get the filament in (you will feel some resistance until it gets past the knurled filament driver wheels), you may have filament residue clogging the Extruder, but that shouldn’t happen if you follow the unload process mentioned earlier. You may also have a bend in your filament. Gently try to straighten it – if you get too aggressive with this, the filament will weaken and be increasingly difficult to insert. If this happens, just spool out some more and snip it off past the weakened area.

6) Once the filament is fully in the extruder, lock the filament lock.

7) Push the tube back in place and use the blue clip to help keep the tube in place.

8) Go to the printer’s user interface and select Extrude.

This will load the filament the rest of the way into the hot end, leaving a small pile of extruded filament on the machine bed that can be removed and thrown away. If you changed colors of filament, the new color should be visible in the extruded filament.

If you hear clicking during the extrude setup, that means either your extruder or the hot end nozzle is clogged and the drive wheels cannot force the filament through. You can try and unload the filament and clear the clog, but you’ll need to use the various tools available to unclog the system or you may need to replace the nozzle, since there could be foreign matter stuck in the nozzle that the little wire cleaning tool cannot push through effectively. Generally, when I get to this stage I replace the nozzle, since it should be recalibrated… after disassembly anyway.

If you didn’t hear clicking and are still not seeing extruded filament, check to ensure that the filament lock is closed. It will not extrude anything if the lock is not closed, engaging the filament driver wheels.

As I mentioned, loading and unloading seems to be a very tedious multi-step process, but it is much better than having a clogged system that you’ll need to disassemble.

Z offset now part of the user interface of the Creality K1

I was out of the country for a while (and got COVID again) so have not been working with my 3D printer as much lately. I did want to provide an update on one of my on-going concerns about the Creality K1 that was addressed by a firmware update.

The latest update to the firmware has been released that can be automatically updated from the K1 (or K1 Max) user interface. It addresses the need for on-the-fly Z offset adjustment.

To access the capability, you need to scroll down the system settings until you see the option for Expert mode:

Once you click on it, you’ll see some new options to optimize:

  • Z offset
  • Flow
  • Nozzle PID calibration

I’ve not had a chance to dig into them much, but Creality now allows on-the-fly adjustment of some additional printer values. This should allow you to adjust the printer’s behavior, rather than to try and make allowances in the slicer(s) that you use.

Dice and marble football game

Last year I made a baseball game for our woodshop sale. It sold in the first 10 minutes of the sale for $40, so I think it may have been underpriced. The baseball game made all the football fans jealous, so now I working on a football game that is similar in format.

I found numerous games online, but they all seemed to have their action one sided – the offense rolls…

The offense gets to select the play category (passing or running), but the defense rolls to determine the impact of the play. Almost every play not involving a penalty is rolled on by both players. The board currently looks like this:

What is needed to play besides the game board?

Two or more people, and a variety of marbles (7 in total), 4 dice and a set of 10 yard down markers:

  1. Two marbles of one color for the home score
  2. Two marbles of a different color for the visitor score
  3. One to count the down
  4. One to count the quarter
  5. One to show field placement of the ball

Some sort of timing mechanism. The length of a quarter should be determined before the game begins. For example, if you decide on 10 minute quarters, the game will be done in close to 40 minutes, since there are no TV time outs…

The down marker ‘chains’ are used to keep track of the 10 yards needed to reach a first down (see more about this in the section of the document covering downs). I 3D printed that element of the game.

The ball placement on the field is shown using diagonal holes across the field.

The rules are fairly inclusive of various game elements like:

  • Onside kick
  • Pass interference
  • Two point conversion
  • Touchback

The rules were a bit more complicated than I would have liked, but football is actually more complicated than I would have though.

A game play example:

The rules go into much more detail about how this works but an example may be nice.

1) The visiting team kicks off and they roll a 17. Since they didn’t roll a 3 or an 18, take 30 and subtract 17. The ball ends up on the receiving team’s 13 yard line. It is 1st and 10 on the 13.

2) The home team (now the offensive team) states that they are going to do a passing play. They roll a 4 and 5. This is a Flea Flicker (+1D). The defensive team rolls a 2 and a 1. The ball advanced 3 yards. It is 2nd down and 7 yards to go on the 16 yard line.

3) The offensive team says they are going to do a running play and rolls a 6 and a 1. That is an Offside penalty (-5). The offense loses 5 yards, and it is still 2nd down. The ball is on the 11, so it is 2nd down and 12 yards to go. Note that to move the ball 5 yards, you just move it back to the hole to the left at the same level.

4) The offense decides to do a passing play. They roll a double 4. This is a Pass Complete (+3D). The defensive team rolls a 5, 3 and a 4 (12). This advances the ball to the 23 yard line, so it is 1st and 10 on the 23.

5) The offense decides to do a passing play and rolls a 1 and a 5. This is an Interception turnover (+1D). The defense rolls one die, a 3. The ball advances in the direction the offense was heading 3 yards, and the ball turns over to the defense. They pick up the ball on the 26 yard line and it is 1st and 10. Not ethat the yard marker is pointed the other direction.

The Vistors (now the offense) are going to do a passing play and roll a 3 and a 6. This is a Pass complete (+1D). The defensive player rolls one die and rolls a 5. The ball advances to the 21 yard line and it is 2nd down and 5 yards to go and it is second down.

Since they are halfway to a first down, they decide to do a running play and roll a 2 and a 3. They are Stopped (0). It is now 3rd down with 5 yards to go, with the ball still on the 21 yard line.

They decide to do a passing play and roll a 3 and a 6, a Pass Complete (+1D). The defense rolls a 2. It is now 4th down with 3 yards to go, the ball is on the 18.

The offense decides they are going to attempt a Field goal. The roll a 6, 2, 1 and a 6 – for 15. They rolled more than 6, so the kick is not blocked. You take the location on the field (18) and subtract the 6 to end up with 12. Subtract 25 from that and you end up with -13. Since that number is less than zero. The field goal was good. It is now Home: 0, Visitors: 3.

The visitors kick off again to the home team and they roll 12. Since they didn’t roll a 3 or an 18, take 30 and subtract 12. The ball ends up on the receiving team’s 18 yard line. It is 1st and 10 on the 18.

The home team is on the offense, and they decide to do a running play. They roll a 6 and 4 – an Off tackle (+1D). The defense rolls a 4. The ball advances to the 22 yard line and it is 2nd down and 6 to go.

The offensive team decides on a running play and rolls double sixes. It is a Touchdown!

The offensive team decides to attempt an extra point. They roll a 5 and a 6 for a total of 11. The defense rolls one die (3). 11 – 3 and end up with 8. Since the result is greater than 2, the extra point is successful.

Home: 7, Visitors: 3 

Creating manual tree supports in Orca

I was having trouble supporting the Catalina Blister. I was creating the supports by hand in my previous post.  One issue was not just supporting the large horizontal elements, but the near vertical elements were just too long, and vibration was making the part surface rough. Another issue was the posts on the outer cowl that the inner mechanism had to fit in and rotate around (see the hole at the bottom of the inner mechanism in the picture). If you have done any work with 3D printing (especially if you have printed a hinge), you’ll know that 3D prints are only strong in two dimensions.

I watched a YouTube video on the new organic capabilities of Orca. It explained the support creation capabilities in detail. One of the features that I investigated was ‘painting’ on the model where you want supports. After just a bit of work, I ended up with the following being generated.

Note how the long vertical elements should be prevented from vibrating much during the build by the supports. Since my printer is now finely tuned (see all the previous posts), I was able to remove the supports with little issue. The technique used was to take some diagonal pliers (snippers) and snip the supports off a branch or two down from where they touch the part, allowing me to apply leverage to only a small part of the model at a time, to snap off the support.

I am happy with the support that was generated after I told Orca where the difficult areas were. It didn’t take up too much plastic or time to create since they are hollow and only a fraction of a mm thick. One thing I did learn was to be careful with the ‘fill’ painting to support, since filling an area will make it difficult to remove the support if ‘the paint’ covers too much.

Shifting to Orca for my slicer

All these issues tuning the Creality K1 (starting with bed leveling) have forced me to reassess the slicer I am using. I was using Prusa but have now shifted over to Orca, one of the Prusa descendants.

Orca’s user interface is a bit more modern and intuitive — at least it has a search capability to find all the settings, if you know what to call them.

There are numerous built in calibration functions:

and some decent documentation on how to use them.

It seems to have all the tweeks I need to adjust to get the more complex prints converted to G Code.

Orca integrates the web interface of my printer, since the K1 has a built in web server:

One thing I did learn was to never click the icon in the upper right that looks like a refresh button (I put a red circle around it). From what I can tell, it tells the printer to stop what it is doing and reboot (or something). It does not appear to be recoverable, so I have just stopped touching it.

Getting the G Code generated over to the printer is still a bit of a manual process, since Orca still coughs up an error when you attempt to print:

I assume it is only a matter of time until they figure it out, since it is definitely working in CrealityPrint. The user interface is there inside Orca:

I have now calibrated the heck out of my installation, so now its off to do some very complex prints that I attempted with Prusa and my old printer.