025 - MicroPython TechNotes: Joystick

Introduction

In this article, we will tackle how to use JOYSTICK with ESP32 using MicroPython programming language.

Pinout

  1. G – for the ground pin.
  2. V – for the supply voltage.
  3. x – for the horizontal potentiometer pin.
  4. y – for the vertical potentiometer pin.

Bill Of Materials

  1. ESP32 development board to serve as the brain for the experiment.
  2. ESP32 shield from Gorillacell ESP32 development kit to extend the ESP32 pins to pin headers for easy circuit connection.
  3. 4-pin female-female dupont jumper wires to connect the joystick module to the ESP32 shield.
  4. Joystick module itself.

Hardware Instruction

  1. First, attach the ESP32 development board on top of the ESP32 shield and make sure that both USB ports are on the same side.
  2. Next, attach the dupont wires to the joystick module by following the color coding which is black for the ground, red for the VCC, yellow for the x pin, and white for the y pin.
  3. Next, attach the other end of the dupont 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, yellow and the following colors to the yellow pin headers.
  4. Next, power the ESP32 shield with an external power supply with a type-C USB connector and make sure that the power switch is set to ON state.
  5. Lastly, connect the ESP32 to the computer through a micro-USB cable. Our demo circuit should be now ready.

Software Instruction

I prepared 3 example source code for this demo.

Copy and paste it to your Thonny IDE.

Play with it, modify and adapt according to your needs.

Enjoy and happy tinkering.

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, basics of reading an analog input just like the joystick module:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin, ADC
 5from time import sleep_ms
 6
 7x = ADC(Pin(32, Pin.IN))
 8y = ADC(Pin(33, Pin.IN))
 9x.atten(ADC.ATTN_11DB)
10y.atten(ADC.ATTN_11DB)
11
12while True:
13    x_val = x.read()
14    y_val = y.read()
15    print('Current position:{},{}'.format(x_val,y_val))
16    sleep_ms(300)

2. Example # 2, display joystick position using RGB matrix:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin, ADC
 5from neopixel import NeoPixel
 6from time import sleep_ms
 7
 8class RGB_Matrix:
 9    
10    def __init__(self, gpio, width, height):
11        self.width = width
12        self.height = height
13        self.neopixel = NeoPixel(Pin(gpio), width*height)
14        
15    def pixel_set(self, row, col, r=0, g=0, b=0):
16        self.neopixel[row + (col*self.width)] = (r, g, b)
17        self.neopixel.write()
18            
19    def pixel_clr(self, row, col):
20        self.neopixel[row + (col*self.width)] = (0, 0, 0)
21        self.neopixel.write()
22        
23    def clear_all(self):
24        self.neopixel.fill((0,0,0))
25        self.neopixel.write()
26
27
28np = RGB_Matrix(25, 8, 8)
29x = ADC(Pin(32, Pin.IN))
30y = ADC(Pin(33, Pin.IN))
31x.atten(ADC.ATTN_11DB)
32y.atten(ADC.ATTN_11DB)
33
34def map(x, in_min, in_max, out_min, out_max): 
35    # This will not handle x value greater than in_max or 
36    #                      x value less than in_min 
37    return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
38
39while True:
40    x_pos = map(x.read(), 0, 4095, 0, 7)
41    y_pos = map(y.read(), 0, 4095, 7, 0)
42    np.clear_all()
43    np.pixel_set(x_pos, y_pos, 10,0,0)
44    sleep_ms(300)

3. Example # 3, control a pixel in RGB matrix using the joystick module:

 1# More details can be found in TechToTinker.blogspot.com 
 2# George Bantique | tech.to.tinker@gmail.com
 3
 4from machine import Pin, ADC
 5from neopixel import NeoPixel
 6from time import sleep_ms
 7
 8class RGB_Matrix:
 9    
10    def __init__(self, gpio, width, height):
11        self.width = width
12        self.height = height
13        self.neopixel = NeoPixel(Pin(gpio), width*height)
14        
15    def pixel_set(self, row, col, r=0, g=0, b=0):
16        self.neopixel[row + (col*self.width)] = (r, g, b)
17        self.neopixel.write()
18            
19    def pixel_clr(self, row, col):
20        self.neopixel[row + (col*self.width)] = (0, 0, 0)
21        self.neopixel.write()
22        
23    def clear_all(self):
24        self.neopixel.fill((0,0,0))
25        self.neopixel.write()
26
27
28np = RGB_Matrix(25, 8, 8)
29x = ADC(Pin(32, Pin.IN))
30y = ADC(Pin(33, Pin.IN))
31x.atten(ADC.ATTN_11DB)
32y.atten(ADC.ATTN_11DB)
33
34curr_x = 3
35curr_y = 3
36
37while True:
38    x_val = x.read()
39    y_val = y.read()
40    
41    if x_val < 1850:  
42      if curr_x > 0:  
43        curr_x = curr_x - 1  
44    elif x_val > 1930:  
45      if curr_x < 7:  
46        curr_x = curr_x + 1  
47    if y_val < 1890:  
48      if curr_y < 7:  
49        curr_y = curr_y + 1  
50    elif y_val > 1970:  
51      if curr_y > 0:  
52        curr_y = curr_y - 1  
53    print(curr_x, curr_y)  
54    
55    np.clear_all()
56    np.pixel_set(curr_x, curr_y, 10, 0, 0)
57    sleep_ms(100)

References And Credits

  1. Purchase 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.