使用雙軸XY搖桿配合Nokia 5110 LCD屏幕嘅Arduino代碼
呢個教學會帶你逐步了解點樣用雙軸XY搖桿去控制Nokia 5110 LCD屏幕上顯示嘅一點。呢個項目涉及讀取搖桿嘅位置同更新顯示。完成呢個指南之後,你就會有一個正常運作嘅設置,移動搖桿會改變屏幕上點嘅位置(喺影片00:00)。

喺呢個項目入面,你會利用搖桿嘅模擬輸出嚟判斷佢喺X同Y軸上嘅位置。由搖桿讀到嘅數值會映射到LCD屏幕嘅尺寸,令點可以順暢移動。另外,你亦會學到點樣讀取搖桿嘅開關輸入,可以用嚟做更多互動。
硬件解說
呢個項目嘅主要零件包括Arduino、雙軸XY搖桿同Nokia 5110 LCD屏幕。Arduino係微控制器,負責處理搖桿嘅輸入同控制LCD顯示。
搖桿通常有兩個電位器——一個用於X軸,一個用於Y軸。當你移動搖桿,呢啲電位器會改變電阻,向Arduino嘅模擬輸入針腳發送唔同嘅電壓水平。Nokia 5110 LCD屏幕係透過SPI通訊控制,可以高效傳輸數據同更新顯示。
數據表詳情
| 製造商 | Texas Instruments |
|---|---|
| 零件編號 | L293D |
| 邏輯/IO電壓 | 5 V |
| 供電電壓 | 4.5–36 V |
| 輸出電流(每通道) | 600 mA |
| 峰值電流(每通道) | 1.2 A |
| PWM頻率建議 | 20 kHz |
| 輸入邏輯閾值 | 兼容TTL |
| 電壓降 / RDS(on) / 飽和 | 最大1.5 V |
| 熱限制 | 150 °C |
| 封裝 | DIP-16 |
| 備註 / 變體 | 廣泛用於馬達驅動應用 |
- 確保連續運作時有適當散熱。
- 使用去耦電容以穩定電源。
- 檢查電壓額定值,避免損壞零件。
- 小心PWM信號;確保喺指定範圍內。
- 檢查接線,避免短路。
接線說明

要將搖桿同LCD屏幕接到Arduino,首先將搖桿嘅VCC針腳接到Arduino嘅5V,GND針腳接到Arduino嘅GND。將搖桿嘅X軸輸出接到模擬針腳A0,Y軸輸出接到模擬針腳A1。搖桿嘅開關輸出應接到數碼針腳2。
對於Nokia 5110 LCD,連接以下針腳:VCC接到5V,GND接到GND,SCE接到針腳7,RST接到針腳6,D/C接到針腳5,DN(MOSI)接到針腳11,SCLK接到針腳13。最後,將LED針腳透過330歐姆電阻接到針腳9以控制背光。
代碼示例及解說
喺setup函數入面,我哋初始化串行通訊同LCD。指令lcdBegin()設置針腳並初始化顯示。對比度會設定到最佳可視性。
void setup() {
Serial.begin(9600);
lcdBegin();
setContrast(40);
delay(1000);
clearDisplay(BLACK);
updateDisplay();
}
呢個初始化項目所需嘅零件,確保LCD準備好顯示資訊。

喺loop函數入面,我哋讀取搖桿嘅位置並映射到屏幕尺寸。點嘅位置會根據搖桿嘅X同Y數值更新。
void loop() {
int x = analogRead(A0); // 讀取搖桿嘅x位置
int y = analogRead(A1); // 讀取搖桿嘅y位置
int xPos = map(x, 0, 1023, 0, screenWidth); // 將x映射到屏幕
int yPos = map(y, 0, 1023, 0, screenHeight); // 將y映射到屏幕
setCircle(xPos, yPos, thickness, BLACK, 2); // 顯示點
updateDisplay();
delay(10);
}
呢段代碼會持續根據搖桿嘅移動更新LCD上點嘅位置。
示範 / 預期效果
當你為電路供電,LCD會顯示一點。移動搖桿會實時根據搖桿嘅X同Y移動重新定位點。如果按下搖桿開關,可以觸發你可能實現嘅額外功能。要注意如果搖桿唔喺中間位置,可能會有浮動輸入,導致點意外移動(喺影片01:30)。
影片時間戳
- 00:00 - 項目介紹
- 01:30 - 接線解說
- 02:45 - 代碼講解
- 04:10 - 功能示範
/*
* This is Arduino code to use a dual-axis XY joystick with a Nokia 5110 screen to move a dot on the screen.
* It also reads the switch.
* Other Arduino libraries and videos: https://robojax.com
* Watch the video for this code to learn it fully.
* Watch the video here: https://youtu.be/zqDZybR5JSE
* This code is offered "as is" without any warranty.
* If you are sharing this code, you must keep this copyright note.
*/
/*
/* Nokia 5100 LCD Example Code with Added Joystick by Robojax
Graphics driver and PCD8544 interface code for SparkFun's
84x48 Graphic LCD.
https://www.sparkfun.com/products/10168
Original source code:
https://github.com/sparkfun/GraphicLCD_Nokia_5110
This stuff could all be put into a library, but we wanted to
leave it all in one sketch to keep it as transparent as possible.
Hardware: (Note most of these pins can be swapped)
Graphic LCD Pin ---------- Arduino Pin
1-VCC ---------------- 5V
2-GND ---------------- GND
3-SCE ---------------- 7
4-RST ---------------- 6
5-D/C ---------------- 5
6-DN(MOSI) ---------------- 11
7-SCLK ---------------- 13
8-LED - 330 Ohm res -- 9
The SCLK, DN(MOSI), must remain where they are, but the other
pins can be swapped. The LED pin should remain a PWM-capable
pin. Don't forget to stick a current-limiting resistor in line
between the LCD's LED pin and Arduino pin 9!
Modified by Ahmad S. for Robojax.com
on Mar 11, 2018 at 20:49 at Ajax, Ontario, Canada
*/
#include <SPI.h>
#include "LCD_Functions.h"
#define sw 2 // pint 2 is used for joystick switch input
#define screenWidth 83 // NOkia screen width
#define screenHeight 47 // Nokia screen height
int thickness =1;// the thickness of the dot
void setup()
{
// Robojax Dual Axis joystick with Nokia 5110 LCD screen project
Serial.begin(9600);
lcdBegin(); // This will setup our pins, and initialize the LCD
//updateDisplay(); // with displayMap untouched, SFE logo
setContrast(40); // Good values range from 40-60
delay(1000);
clearDisplay(BLACK);
updateDisplay();
}
void loop()
{
clearDisplay(WHITE);
// Robojax Dual Axis joystick with Nokia 5110 LCD screen project
int x = analogRead(A0);// read the x position of joystick
int y = analogRead(A1); // read the y position of joystick
int xPos = map(x, 0, 1023,0,screenWidth);// map or translate the x of joystick to x of screen
int yPos = map(y, 0, 1023,0,screenHeight); // map or translate the y of joystick to y of screen
int sStat = digitalRead(sw);//read the switch from pin 2
Serial.print("X: ");
Serial.print(xPos);
Serial.print(" Y: ");
Serial.println(yPos);// Robojax prints y
// Robojax Dual Axis joystick with Nokia 5110 LCD screen project
//setCircle(xPos, yPos, thicness, BLACK, 2);
// xPos is the position of x
// yPos is the position of y
// thickness
setCircle(xPos, yPos, thickness, BLACK, 2);// display the dot on the screen
updateDisplay();
delay(10);
}
你可能需要嘅嘢
-
亞馬遜
-
亞馬遜Purchase XY joystick from Amazonamzn.to
資源與參考資料
暫時冇資源。
文件📁
所需文件 (.h)
-
robojax-nokia5110.h header file for Arduino
robojax-nokia5110.zip0.01 MB -
LCD_Functions. file for Nokia
LCD_Functions.h0.02 MB
Fritzing 文件
-
Nokia 5110 LCD
Nokia_5110_LCD.fzpz0.03 MB -
Black Joystick KY-023
Black Joystick KY-023.fzpz0.02 MB