# What is this TTL/PWM stuff about?

The reason for writing this article is the post about me building a laser engraver. I decided to make this an article of its own, because it might be interesting enough for other purposes.

## TTL

Digging in a bit further when investigating why my laser module claims to support both TTL and PWM (how two?) I found that TTL is just a digital signal with 5 volt being HIGH and 0 volt being LOW.

Well, that’s actually oversimplifying things, in reality there are some thresholds like anything up to 0,4 volt being LOW and anything above 2,4 volts being HIGH or something along those lines according to wikipedia. At least conceptually however, 0 volt is LOW and 5 volt is HIGH.

Translating this to a laser it would mean that providing it with 0 volts would have it turned off and providing it with 5 volts would turn it on.

## PWM

PWM is a way to modulate intensity using a digital signal when controlling for example an LED or a motor or something else I haven’t imagined. It will definitely not work for all devices, but motors and LED’s are good examples of what works.

Basically PWM allows you to drive the brightness of an LED or the speed of a motor. It does so using a digital signal that is turned LOW or HIGH over time and hence you could say that it uses TTL (at least when the voltage is 5 volt, for other voltages I guess it is not TTL in a pure sense?).

If we want an LED to be completely lit we will supply it the HIGH voltage. When we want it completely turned off we supply the LOW voltage (0 volt). If we want to control the brightness we give a signal that varies between LOW and HIGH over time. So if we want a brightness of 50% that means we give it a signal that is LOW 50% of the time and HIGH the other 50% of the time. This works because we repeat this really fast. The process of turning it on and off is in a very small time frame.

To illustrate what PWM does I have created a simulation using tinkercad. It shows an LED fading from off to on. On the right side of the LED it shows the signal on a simulated oscilloscope, which is a square wave.

By ranging the width of the block in the square wave we control the amount of time it is LOW or HIGH and thus when it is on or off. By doing this really fast we can control brightness, speed or other stuff I haven’t imagined.

The code for the Arduino is:

```int led_port = 9;   // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by

void setup() {
pinMode(led_port, OUTPUT);
}

void loop() {
analogWrite(led_port, brightness);

brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
}
delay(5);
}
```

# Building a Laser Engraver

In 2019 I started building a laser engraver. Here I will document some of the joys and  problems I faced in the process.

## Base Design

A friend of mine pointed me to a 3D printable laser engraver design (here), which I used as a starting point. The design uses some 2020/2040 aluminium profile and 3D printed parts as a base. Part of the design is to shove the aluminium profiles in some of the 3D printed parts to join them at the corners and create a rectangular shaped base.

After printing the parts and trying to assemble it, some printed parts immediately broke. I immediately disliked this part of the design and didn’t quite understand why one would take this approach when 2040 aluminium can be bolted together easily, which is way more solid and wouldn’t lead to breaking 3D printed parts. So I decided to modify that part of the design and simply bolt the aluminium and design some new ‘feet’ for the construction. This helped and I could mostly use the rest of the original design.

## Modifying the Design

I had to design some new 3D printable parts and made good progress in getting the base together. By base I mean the whole construction with the stepper motors and the timing belt used to drive a laser on two axes.

In the first months I was able to create a base that I liked and get the mechanical parts working. I had the stepper motors working and could drive the position of the laser using G-CODE, so I could basically use any laser engraving software to control the device. This was the fun and satisfying part. The hard part came when I had to drive the laser itself.

## Controller Board

Another part about the design that I wanted to modify is the controller. I didn’t want to use an Arduino Uno and after reading about a controller board built by Bart Dring, I decided to go with that because it uses an ESP32 microcontroller.

When receiving the board it looked like this:

And after installing the ESP32 microcontroller and the stepper drivers it looks like this:

The X,Y and Z-axis stepper motors each have their own driver and SpinPWM is the port that controls the laser. Because an ESP32 is the microcontroller it has WIFI and Bluetooth, which is pretty cool. It also has an SD card slot and it has some connections for limiters (end stops) and other things that I may not fully grasp yet. But that is something for the future.

## Driving the Laser

I ordered the strongest diode laser I could find on AliExpress, a 15 watt laser module, because why settle for less?

According to its specifications it would operate on 12 volts and take a PWM or TTL signal at 5 volts. This confused the hell out of me to be honest. PWM or TTL? Is that the same then? I understand what PWM (Pulse Width Modulation) is and does, but to be honest I have no clue about how TTL (Transistor-Transistor Logic) works.

I had hooked up the laser to the SpinPWM port and after hooking things up, nothing worked. So I used my cheap multimeter to measure the voltage and was a bit surprised, it was so low (not sure what it was anymore, but at most around 3.3 volts). At this point I couldn’t be sure what was going on.

At this point I realized I didn’t have the right tools for the job, because I wasn’t quite sure what the signal was giving me and a simple multimeter will not help me figure it out. At this moment I decided to let this whole project rest for a bit and order myself a cheap oscilloscope (a DSO138).

When the oscilloscope and some spare time came along it was time to do some investigation. Like, what is this TTL/PWM stuff about?