DIY laser projector part 2: vector graphics

This post is a little wordy I’m afraid. Anyone with a reasonable knowledge of oscilloscopes and Arduinos can probably skim read it. I promise future posts will be more concise but I need to start with the basics.

DSCF3074

Before we start playing around with lasers, we need to understand what we are actually trying to do.

This entire project started because i bought an old oscilloscope from Ebay (above). I have always been terrible at understanding electronics and I wanted to learn more. Obviously i got distracted pretty quickly which led to me starting this whole project in the first place.

So, what is an oscilloscope?

Note, text in red can be skipped if you already have a knowledge of this subject!

An oscilloscope is basically a seriously useful piece of electronics equipment. They look complicated but are actually really simple. There are 3 main components:

  • An electron beam – this is an invisible beam of electrons which is fired towards the screen.
  • The screen – when the electron beam hits a point on the screen, that point glows, making a glowing dot on the screen.
  • The input connector(s) – most scopes have two of these (I’ll be calling them ‘X’ and ‘Y’). Let’s only worry about a single one for now. If we feed a voltage into the input connector, the electron beam, and therefore the dot on the screen will move, depending on how high the voltage is.

This doesn’t sound too useful yet, but oscilloscopes have two main modes which let us do some useful things with the dot:

Sweep mode

This causes the dot to automatically move repeatedly across the screen. By changing the scope settings, you can control the speed of the dot. If you then apply a voltage to input connector Y, the dot will jump up, according to that voltage. If the voltage is alternating (aka AC), you will get a wavy line. Although this is the most commonly used mode, we won’t be using it much here.

XY mode

This is more relevant to us. With the oscilloscope in XY mode, the dot will stay still in the middle of the screen by default. But:

  • If we feed a fixed voltage into the ‘X’ connector, the dot will move horizontally. The higher the voltage, the further the dot moves.
  • Likewise, if we feed a voltage into the ‘Y connector, the dot will move vertically. The higher the voltage, the further the dot moves.

So, with the scope set in XY mode, the dot can be moved to any position on the screen as long as we can provide a range of voltages to the input connectors.

scope

The main parts of an oscilloscope

Vector graphics on an oscilloscope – intro

Much like a laser (as mentioned in my previous post) a dot on an oscilloscope screen appears as a line if it is moved back and forth repeatedly at high speed. In other words, if we can control where the dot goes, we can trace out any line image – it’s just like drawing something by hand with a pencil.

The technical term for this kind of image is a vector graphic. Most people are familiar with them in the form of old (awesome) arcade games like asteroids. These were literally big versions of an oscilloscope in XY mode where the beam drew out the image on the screen.

When I started this project, I was trying to draw some basic vector graphics on an oscilloscope screen. I hadn’t even thought of lasers yet.

Putting the dot where we want it

So, we know that by applying the correct voltages to the X and Y connectors, we can move the dot to any position on the scope screen. Let’s look at some examples (there will be plenty of my rubbish hand drawings coming up):

First, let’s set the scope up in XY mode. Let’s also set it so the dot sits at the bottom left hand side and bottom of the screen when no voltage is applied to the connectors.

Let’s then set the scale so that an input of 5v on the x connector will move the dot to the far right hand side of the screen. Likewise, an input of 5v on the y connector will move the dot to the top of the screen.

With no voltage applied to either connector, the screen looks like this:

DSCF3080

scope switched on and set up as described above. The dot sits in the bottom left of the screen

Now let’s wire up a couple of power supplies to the X and Y connectors like this:

Note, the ‘GND connector is just connected to zero volts.

DSCF3075

Wiring the scope up to two power sources

So, we have 3v on each of the inputs.

For the X input:

  • A voltage of 0v would cause the dot to stay at the far left of the screen.
  • A voltage of 5v would cause the dot to move to the far right of the screen.
  • So, a voltage of 3v should cause the dot to move 3/5 of the way across the screen.

And the same applies for the Y input, but vertically:

  • A voltage of 0v would cause the dot to stay at the  bottom of the screen.
  • A voltage of 5v would cause the dot to move to the top of the screen.
  • So, a voltage of 3v should cause the dot to move 3/5 of the way aup the screen.

So, we expect to see the dot 3/5 of the way across and 3/5 of the way up the screen:

DSCF3082

And that’s exactly what we get. So, applying any voltage between 0V and 5V to the X and Y inputs, we can move the dot anywhere on the screen.

Speeding things up

Obviously to draw anything useful we need to be able to move the dot around quickly. In other words, we need to change the voltages on the connectors quickly.

Imagine applying 1V to the X connector and then switching this to 4V and repeating his many times per second. The dot would move from 1/5 of the way across the screen to 4/5 of the way across rapidly again and again. It would look like a horizontal line to us.*

*This isn’t actually how we draw lines. We’ll see why later but let’s keep things simple for now.

So how are we going to:

  • record what voltages (ie dot positions) we feed into the connectors.
  • switch the voltages rapidly from one to the next to draw lines?

What we need is a computer programme which outputs a ‘list’ of changing voltages to the connectors. 1 year ago, the thought of programming made me feel physically sick. Fortunately, there is an absolutely incredible piece of kit which can be used for pretty much any project that can be dreamt up. A child can learn to use it and many have. It is personally my favourite invention of the last 10 years…

More red text, just skip if you know this stuff.

The arduino

I’m not going to write a detailed description of what an arduino is and what it can do – plenty people have already done that (see links below).

Arduino with main components labelled

Arduino with main components labelled

In short, it’s a microprocessor connected to a series of output pins. You write a programme in the relevant code (also called ‘Arduino’) and the output pins will generate a voltage between 0V and 5V as instructed. In order to program the device, you just connect it to your computer via USB and write your program in the Arduino software.

There are also input pins, but we aren’t concerned about those for this project.

I started to learn to code in Arduino about 6 months ago and this is my first successful major project (I’ll post some of the smaller projects at a later date). I think most people would be able to learn a lot quicker than me, but unfortunately programming just does not come naturally to me at all.

I’ll gradually be explaining how my code works but I won’t be going into the actual details of coding for two reasons:

  • That would take up half the blog.
  • My code is absolutely awful and inefficient. Sure it works, but I don’t want anyone else developing my bad habits.

Sending a voltage from the arduino to the oscilloscope

All we really need to know is that the output pins on the arduino can work in two ways:

  • We can use digitalWrite to output either 5V or 0V from a specified pin. This method cannot write intermediate voltages. So, for example, to output 5V from pin number 3, we would use the digitalWrite function to write HIGH to pin 3.
  • We can use analogWrite to write any voltage between 0V and 5V to a pin. This fuction works by us telling the pin to write a value between 0 and 255 where 0= 0V and 255=5V. So, for example, to write 2.5V to pin number 5, we would use the analogWrite function to write 128 to pin 5 (128 is half of 255, so it gives 2.5V which is half of 5V).

In general, to write a desired voltage using analogWrite we use this formula:

analogWrite value = (desired voltage/255)*5

It looks like we’ll be using the analogWrite function exclusivley as this will output any voltage we like to the oscilloscope connectors. So, we should be able to move the dot anywhere we want.

Let’s wire up the arduino to the oscilloscope:

DSCF3077

wiring the arduino to the scope in order to move the dot using analogWrite

Before we try and draw a line with it as described above (by moving the dot quickly), let’s use the arduino to put the dot in the middle of the screen.

So, we need to apply 2.5V to the x connector and 2.5V to the y connector.

To do this, we need pin 3 to output 2.5V and pin 5 to output 2.5V.

Here is the code to do that:

//code to write 2.5v to two arduino pins using analogWrite
//note anything prefixed by '//' is a cmment and not part of the code
void setup() {
 
 //this tells the arduino to assign pins 3 and 5 as outputs
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
}
void loop() {//loop so the code repeats over and over
 //this writes 2.5v to each pin
analogWrite(3,128);
analogWrite(5,128);
}

And here’s what happens when we run this code:

Scope in XY mode with 2.5v applied to each input connector using analogWrite. Note, the display has been re-scaled; each of the dots show voltages of either 5V or 0V.

Scope in XY mode with 2.5v applied to each input connector using analogWrite. Note, the display has been re-scaled; each of the dots show voltages of either 5V or 0V.

!!?

What happened there?

I’ve been a little misleading, it turns out that when you output voltages on the pins of the Arduino using analogWrite, the voltage isn’t actually constant. It uses something called…

Pulse Width Modulation

The output pins on an arduino are only actually physically capable of supplying 0V or 5V. So, if we want to output an intermediate value, the device has to use a sort of compromise. It switches very quickly between 0V and 5V. Like this:

Pulse Width Modulation - how it simulates a voltage

Pulse Width Modulation – how it simulates a voltage

We can even see this if we set the oscilloscope to sweep mode and output 2.5V to connector x using analogWrite:

Displaying the actual voltage over time when pulse width modulation is used (see above diagram)

Displaying the actual voltage over time when pulse width modulation is used (see above diagram). Note, the scope is in sweep mode in this case. Again, I’d been fiddling around with the scope scale settings so each line represents either 5V or 0V

For most applications this would be fine. If we were using the Arduino to light and LED, the voltage change would be so fast that we wouldn’t be able to see it flickering – it would just look like an LED with a steady 2.5V flowing through it.

But, for drawing thisgs on an oscilloscope this is useless. The voltage pulses cause the dot to flicker about all over the place so we can’t make an image.

So we need to find another way of providing precise voltages to the oscilloscope very quickly using the Arduino. The solution is really cool and incredibly simple and I’ll be covering it in a few days…

Summary – where are we?

  • We know what an oscilloscope is and how XY mode works.
  • We can make a stationary dot appear anywhere on the oscilloscope screen using a power supply as the input.
  • We want to move this dot around quickly using signals from and Arduino as the input. But, analog output signals from the Arduino are ‘distorted’ because of pulse width modulation. We need to find another way.

Sources and further reading

Beginners guide to oscilloscopes: https://learn.sparkfun.com/tutorials/how-to-use-an-oscilloscope

A list of old video games which use vector graphics: https://en.wikipedia.org/wiki/Category:Vector_arcade_games

The arduino website. The support and forums are amazing for finding out anything you could ever want to know about the arduino: https://www.arduino.cc/

More info on pulse width modulation for the arduino: https://www.arduino.cc/en/Tutorial/PWM

More info on basic arduino programs (actually called sketches). Gives an idea on how to start programming: https://www.arduino.cc/en/Tutorial/Sketch

Advertisements

2 thoughts on “DIY laser projector part 2: vector graphics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s