017 - MicroPython TechNotes: LASER Module

Introduction

In this article, we will learn on how to use a LASER module with ESP32 using MicroPython programming language.

Pinout

  1. G – for the ground pin.
  2. V – for the supply voltage.
  3. S – for the control signal pin.

Bill Of Materials

  1. ESP32 development board with MicroPython firmware that will serve as the main microcontroller.
  2. ESP32 shield from Gorillacell ESP32 development kit to extend the pins of ESP32 and to provide easy circuit connection.
  3. 3-pin female-female dupont jumper wires.
  4. And of course the LASER module itself.

Hardware Instruction

  1. First, attach the ESP32 development board on top of the ESP32 shield and make sure that both USB port are on the same side.
  2. Next, attach the dupont wires to the LASER module by following the color coding which black for the ground, red for the VCC, and yellow for the control signal pin.
  3. Next, attach the other side of the dupont jumper wires to the ESP32 shield by matching the colors of the wires to the colors of the pin headers which is black to black, red to red, and yellow to yellow. For this experiment, I choose GPIO 25 because this ESP32 shield provides 5V on GPIO 25.
  4. Next, power the ESP32 shield with an external power supply with type-C USB connector. Make sure that the power switch is set to ON state.
  5. Next, connect the ESP32 to the computer through a micro-USB cable. The demo circuit should be ready.

Software Instruction

For this experiment, I prepared 2 example source code. Just copy and paste it in Thonny. Enjoy.

Video Demonstration

Call To Action

For any concern, write your message in the comment section.

You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.

Thank you and have a good days ahead.

See you,

– George Bantique | tech.to.tinker@gmail.com

Source Code

1. Example # 1, basic and simple control of LASER module:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin
 5
 6laser = Pin(25, Pin.OUT)
 7
 8# # The following source should be tested using the REPL:
 9# laser.on()  # Turns ON the laser.
10# laser.off() # Turns OFF the laser.

How the code works:

from machine import Pin Loads or imports the necessary library, in this case the Pin class from the machine module. This is in order to access the pins of ESP32.

laser = Pin(25, Pin.OUT) This line of code creates the object named laser which is connected on GPIO 25 with a pin direction set as an output.

laser.on() This will set the pin to logic HIGH which will turn ON the laser module.

laser.off() This will clear the pin to logic LOW which will turn OFF the laser module.

2. Example # 2, SOS using the LASER module:

 1
 2# More details can be found in TechToTinker.blogspot.com 
 3# George Bantique | tech.to.tinker@gmail.com
 4
 5from machine import Pin
 6from time import ticks_ms
 7
 8laser = Pin(25, Pin.OUT)
 9switch = Pin(0, Pin.IN, Pin.PULL_UP)
10
11DOTS_INTERVAL = 250
12DASH_INTERVAL = 750
13LOWS_INTERVAL = 500
14BETW_INTERVAL = 700
15
16isSOS = False
17startTime = ticks_ms()
18steps = 0
19
20while True:
21    if isSOS:
22        if steps == 1 or steps == 3 or steps == 5:
23            laser.on()
24            if ticks_ms() - startTime >= DOTS_INTERVAL :
25                steps += 1
26                startTime = ticks_ms()
27        elif steps == 7 or steps == 9 or steps == 11:
28            laser.on()
29            if ticks_ms() - startTime >= DASH_INTERVAL:
30                steps += 1
31                startTime = ticks_ms()
32        elif steps == 0 or steps == 6 or steps == 12:
33            laser.off()
34            if ticks_ms() - startTime >= BETW_INTERVAL:
35                steps += 1
36                startTime = ticks_ms()
37        else:
38            laser.off()
39            if ticks_ms() - startTime >= LOWS_INTERVAL:
40                steps += 1
41                startTime = ticks_ms()
42         
43        if steps > 12:
44             steps = 0
45        
46    if switch.value() == 0:
47        laser.off()
48        #print('press')
49        isSOS = not isSOS
50        steps = 0
51        sleep_ms(300)

References And Credits

  1. Purchased your Gorillacell ESP32 development kit at: gorillacell.kr


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.