ステッピングタコメータを作成するときに、机上で動作テストを行う際に必要な簡易装置です。
Arduino uno R3で作成しています。
ソースをコンパイルしてR3に書き込み、起動するとマニュアルモードで立ち上がります。
操作方法
A. 通常操作(マニュアルモード)
a. ボリュームを回すとリアルタイムに回転数とHzが変化します。
B. プリセットの呼び出しと保存
a. SW1〜SW3を「短く」押すと、保存されている回転数を呼び出します。
b. マニュアルモードで任意の回転数に合わせた後、SW1〜SW3を「長押し(約2秒)」すると、画面に「Saved!」とモードの位置に表示され、現在の回転数が上書き保存されます。
C. 自動スイープ機能
a. SW4を短く押すと、自動で回転数が上下するスイープモードになります。もう一度押すとマニュアルモードに戻ります。 (0~15000rpmを50rpm毎で移動するので時間がかかります。)
D. 点火パルス係数の設定モード
a. 電源を入れる際、SW4を押しながら起動してください。
b. 設定モードに入り、ボリュームを回すことでクランク1回転あたりのパルス数(0.5、1.0、2.0など)に変更できます。
c. SW4を長押しすると設定が保存されますので、電源を入れなおして再起動してください。
必要な機材は
Arduino uno R3
LCDディスプレイ(1602)とI2Cモジュール
タクトスイッチ4個
50KΩ B ボリューム1個です。
回路の配線手順
A. LCDディスプレイ(I2C接続)の配線
a. VCCをArduinoの「5V」へ接続します。
b. GNDをArduinoの「GND」へ接続します。
c. SDAをArduinoの「A4」ピンへ接続します。
d. SCLをArduinoの「A5」ピンへ接続します。
B. タクトスイッチの配線(4個すべて同じ方法です)
a. SW1(プリセット1用):一方を「D2」ピンへ、もう一方を「GND」へ接続します。
b. SW2(プリセット2用):一方を「D3」ピンへ、もう一方を「GND」へ接続します。
c. SW3(プリセット3用):一方を「D4」ピンへ、もう一方を「GND」へ接続します。
d. SW4(スイープ・設定用):一方を「D5」ピンへ、もう一方を「GND」へ接続します。
C. ボリュームとパルス出力の配線
a. ボリュームは「5V」「GND」「A0」に接続します。(3配線の両サイドに5VとGNDをそれぞれ、真ん中にA0を繋ぎます。)
b. パルス出力は「9」番ピンから取得します
上記の仕様をもとに書いた、ソースコード
(Arduino IDE 2.3.10 LiquidCrystal_I2Cは2.0.0を使用)
// ============================================================
// tacho_plus_generator_final 2026/09/29
// Arduino UNO R3
// タコメータ試験用 信号発生器
//
// D9 : 5V 矩形波出力
//
// 機能
// ・マニュアルモード 0~15000 rpm
// ・プリセット Sw1~Sw3
// ・オートスイープ Sw4
// ・EEPROM保存
// ・1クランクXパルス設定 0.5 / 1 / 2 / 4
// ・約50%デューティ
//
// Arduino UNO R3
// F_CPU = 16MHz
//
//
// ============================================================
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
// ============================================================
// ピン割り当て
// ============================================================
const byte pulsePin = 9;
const byte potPin = A0;
const byte sw1Pin = 2;
const byte sw2Pin = 3;
const byte sw3Pin = 4;
const byte sw4Pin = 5;
// ============================================================
// LCD
// ============================================================
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ============================================================
// 定数
// ============================================================
const int minRPM = 0;
const int maxRPM = 15000;
const byte MAGIC_NUMBER = 0xA7;
// ============================================================
// Timer1
// ============================================================
//
// UNO R3
// F_CPU = 16MHz
//
// Timer1をCTCモードで使用。
// OC1A(D9)を比較一致ごとにハードウェアトグルする。
//
// 周波数:
//
// Fout = F_CPU /
// (2 × prescaler × (OCR1A + 1))
//
// 今回はPrescaler = 64
//
// 15,000rpm / 0.5PPR = 125Hz
// 15,000rpm / 1.0PPR = 250Hz
// 15,000rpm / 2.0PPR = 500Hz
// 15,000rpm / 4.0PPR = 1000Hz
//
//
// ============================================================
const uint32_t TIMER1_CLOCK = 16000000UL;
const uint16_t TIMER1_PRESCALER = 64;
const uint8_t TIMER1_CS_BITS =
_BV(CS11) | _BV(CS10);
// ============================================================
// グローバル変数
// ============================================================
float pulsesPerRev = 1.0;
int presetRPM[3] =
{
1500,
9500,
10700
};
// 0 = Manual
// 1 = Preset 1
// 2 = Preset 2
// 3 = Preset 3
// 4 = Auto Sweep
byte currentMode = 0;
float currentFreq = 0.0;
int currentRPM = 0;
// ============================================================
// スイープ
// ============================================================
int sweepRPM = 0;
bool sweepUp = true;
unsigned long lastSweepTime = 0;
// ============================================================
// ボタン
// ============================================================
struct ButtonState
{
byte pin;
bool lastState;
unsigned long pressTime;
bool isPressed;
bool longPressHandled;
};
ButtonState btn1 =
{
sw1Pin,
HIGH,
0,
false,
false
};
ButtonState btn2 =
{
sw2Pin,
HIGH,
0,
false,
false
};
ButtonState btn3 =
{
sw3Pin,
HIGH,
0,
false,
false
};
ButtonState btn4 =
{
sw4Pin,
HIGH,
0,
false,
false
};
// ============================================================
// Timer1周波数変更用
// ============================================================
//
// loop()から直接OCR1Aを書き換えない。
//
// 新しい値をpendingOCR1Aに保存し、Timer1の比較一致ISRで
// 次の周期に適用する。
//
// Timer1そのものは停止しない。
// ============================================================
volatile uint16_t pendingOCR1A = 0;
volatile bool pendingFrequencyChange = false;
// 現在のOCR1A
uint16_t currentOCR1A = 0;
// 出力停止状態
bool pulseOutputEnabled = false;
// ============================================================
// EEPROM
// ============================================================
//
// 0 : MAGIC_NUMBER
// 1 : pulsesPerRev (float)
// 5 : presetRPM[0]
// 7 : presetRPM[1]
// 9 : presetRPM[2]
// 11 : currentMode
//
// ============================================================
// ============================================================
// Timer1初期化
// ============================================================
void setupTimer1()
{
noInterrupts();
// Timer1停止
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
// 初期値
OCR1A = 249;
currentOCR1A = 249;
// CTC
//
// COM1A0 = 1
// → OC1A(D9)を比較一致ごとにトグル
//
// WGM12 = 1
// → CTC
TCCR1A =
_BV(COM1A0);
TCCR1B =
_BV(WGM12) |
TIMER1_CS_BITS;
// Compare A割り込み
TIMSK1 =
_BV(OCIE1A);
// 最初は出力停止
TCCR1A &=
~_BV(COM1A0);
PORTB &=
~_BV(PORTB1);
pulseOutputEnabled = false;
interrupts();
}
// ============================================================
// Timer1 Compare A ISR
// ============================================================
//
// Timer1を停止しない。
// TCNT1もリセットしない。
//
// ============================================================
ISR(TIMER1_COMPA_vect)
{
if (pendingFrequencyChange)
{
OCR1A = pendingOCR1A;
currentOCR1A =
pendingOCR1A;
pendingFrequencyChange =
false;
}
}
// ============================================================
// パルス出力停止
// ============================================================
//
// Timer1自体は停止させる。
// 「0rpm」のときだけ使用。
//
// 通常の周波数変更ではこの関数を呼ばない。
// ============================================================
void stopPulseOutput()
{
noInterrupts();
// OC1A切断
TCCR1A &=
~_BV(COM1A0);
// LOW
PORTB &=
~_BV(PORTB1);
pulseOutputEnabled =
false;
interrupts();
}
// ============================================================
// パルス出力開始
// ============================================================
void startPulseOutput()
{
noInterrupts();
// OC1Aハードウェアトグル
TCCR1A |=
_BV(COM1A0);
pulseOutputEnabled =
true;
interrupts();
}
// ============================================================
// 周波数設定
// ============================================================
//
// 1. 新しいOCR1Aを計算
// 2. pendingOCR1Aへ保存
// 3. 次の比較一致ISRでOCR1Aを変更
//
// Timer1は連続動作する。
//
// ============================================================
void setPulseFrequency(float freq)
{
// ----------------------------------------------------------
// ほぼ0Hzなら出力停止
// ----------------------------------------------------------
if (freq < 1.9)
{
if (pulseOutputEnabled)
{
stopPulseOutput();
}
return;
}
// ----------------------------------------------------------
// 必要なOCR1Aを計算
// ----------------------------------------------------------
float countFloat =
(float)TIMER1_CLOCK /
(
2.0 *
(float)TIMER1_PRESCALER *
freq
);
uint32_t count =
(uint32_t)(
countFloat + 0.5
);
if (count < 1)
{
count = 1;
}
if (count > 65536UL)
{
count = 65536UL;
}
uint16_t newOCR1A =
(uint16_t)(count - 1);
// ----------------------------------------------------------
// まだ出力していない場合
// ----------------------------------------------------------
if (!pulseOutputEnabled)
{
noInterrupts();
OCR1A =
newOCR1A;
currentOCR1A =
newOCR1A;
pendingFrequencyChange =
false;
// OC1A開始
TCCR1A |=
_BV(COM1A0);
pulseOutputEnabled =
true;
interrupts();
return;
}
// ----------------------------------------------------------
// 現在と同じなら変更不要
// ----------------------------------------------------------
if (
newOCR1A ==
currentOCR1A
)
{
return;
}
// ----------------------------------------------------------
// 新しい値を予約
//
// Timer1は止めない。
// ----------------------------------------------------------
noInterrupts();
pendingOCR1A =
newOCR1A;
pendingFrequencyChange =
true;
interrupts();
}
// ============================================================
// RPM → 周波数
// ============================================================
float rpmToFreq(int rpm)
{
return
(
(float)rpm *
pulsesPerRev
) / 60.0;
}
// ============================================================
// 周波数 → RPM
// ============================================================
int freqToRPM(float freq)
{
return
(int)
(
(freq * 60.0) /
pulsesPerRev
);
}
// ============================================================
// SETUP
// ============================================================
void setup()
{
pinMode(
pulsePin,
OUTPUT
);
pinMode(
sw1Pin,
INPUT_PULLUP
);
pinMode(
sw2Pin,
INPUT_PULLUP
);
pinMode(
sw3Pin,
INPUT_PULLUP
);
pinMode(
sw4Pin,
INPUT_PULLUP
);
Serial.begin(115200);
lcd.init();
lcd.backlight();
// Timer1
setupTimer1();
// ----------------------------------------------------------
// SW4を押しながら起動
// ----------------------------------------------------------
if (
digitalRead(sw4Pin) ==
LOW
)
{
settingsMode();
}
// ----------------------------------------------------------
// EEPROM
// ----------------------------------------------------------
byte magic =
EEPROM.read(0);
if (
magic !=
MAGIC_NUMBER
)
{
EEPROM.put(
1,
pulsesPerRev
);
EEPROM.put(
5,
presetRPM[0]
);
EEPROM.put(
7,
presetRPM[1]
);
EEPROM.put(
9,
presetRPM[2]
);
EEPROM.put(
11,
currentMode
);
EEPROM.write(
0,
MAGIC_NUMBER
);
}
else
{
EEPROM.get(
1,
pulsesPerRev
);
EEPROM.get(
5,
presetRPM[0]
);
EEPROM.get(
7,
presetRPM[1]
);
EEPROM.get(
9,
presetRPM[2]
);
EEPROM.get(
11,
currentMode
);
}
// ----------------------------------------------------------
// 起動時 前回のモード優先 0に設定
// ----------------------------------------------------------
currentRPM =
0;
currentFreq =
0.0;
setPulseFrequency(
0.0
);
updateDisplay();
}
// ============================================================
// LOOP
// ============================================================
void loop()
{
handleButtons();
// ==========================================================
// モード
// ==========================================================
switch (
currentMode
)
{
// --------------------------------------------------------
// Manual
// --------------------------------------------------------
case 0:
{
static int lastPotValue = 0;
int potValue =
analogRead(
potPin
);
// 小さなADC変動を無視
if (
abs(
potValue -
lastPotValue
) >= 2
)
{
lastPotValue =
potValue;
}
currentRPM =
map(
lastPotValue,
0,
1023,
minRPM,
maxRPM
);
currentFreq =
rpmToFreq(
currentRPM
);
setPulseFrequency(
currentFreq
);
}
break;
// --------------------------------------------------------
// Preset 1~3
// --------------------------------------------------------
case 1:
case 2:
case 3:
{
currentRPM =
presetRPM[
currentMode - 1
];
currentFreq =
rpmToFreq(
currentRPM
);
setPulseFrequency(
currentFreq
);
}
break;
// --------------------------------------------------------
// Auto Sweep
// --------------------------------------------------------
case 4:
{
if (
millis() -
lastSweepTime >
20
)
{
lastSweepTime =
millis();
if (sweepUp)
{
sweepRPM += 50;
if (
sweepRPM >=
maxRPM
)
{
sweepRPM =
maxRPM;
sweepUp =
false;
}
}
else
{
sweepRPM -= 50;
if (
sweepRPM <=
minRPM
)
{
sweepRPM =
minRPM;
sweepUp =
true;
}
}
currentRPM =
sweepRPM;
currentFreq =
rpmToFreq(
currentRPM
);
setPulseFrequency(
currentFreq
);
}
}
break;
}
// ==========================================================
// LCD
// ==========================================================
static unsigned long
lastDisplayUpdate = 0;
if (
millis() -
lastDisplayUpdate >
100
)
{
lastDisplayUpdate =
millis();
updateDisplay();
}
}
// ============================================================
// ボタン処理
// ============================================================
void handleButtons()
{
checkButton(
&btn1,
1
);
checkButton(
&btn2,
2
);
checkButton(
&btn3,
3
);
checkButton(
&btn4,
4
);
}
// ============================================================
// ボタン状態確認
// ============================================================
void checkButton(
ButtonState* btn,
int btnNum
)
{
bool currentState =
digitalRead(
btn->pin
);
static unsigned long
lastEdgeTime[5] =
{
0,
0,
0,
0,
0
};
if (
currentState !=
btn->lastState
)
{
if (
millis() -
lastEdgeTime[btnNum] <
50
)
{
return;
}
lastEdgeTime[btnNum] =
millis();
}
else
{
// --------------------------------------------------------
// 長押し
// --------------------------------------------------------
if (
currentState == LOW &&
btn->isPressed
)
{
if (
btnNum >= 1 &&
btnNum <= 3
)
{
if (
millis() -
btn->pressTime >
1500 &&
!btn->longPressHandled
)
{
btn->longPressHandled =
true;
handleLongPress(
btnNum
);
}
}
}
return;
}
// ----------------------------------------------------------
// エッジ検出
// ----------------------------------------------------------
if (
currentState == LOW
)
{
btn->pressTime =
millis();
btn->isPressed =
true;
btn->longPressHandled =
false;
}
else
{
if (
btn->isPressed &&
!btn->longPressHandled
)
{
handleShortPress(
btnNum
);
}
btn->isPressed =
false;
}
btn->lastState =
currentState;
}
// ============================================================
// 短押し
// ============================================================
void handleShortPress(
int btnNum
)
{
if (
btnNum >= 1 &&
btnNum <= 3
)
{
if (
currentMode ==
btnNum
)
{
currentMode =
0;
}
else
{
currentMode =
btnNum;
}
EEPROM.put(
11,
currentMode
);
}
else if (
btnNum == 4
)
{
if (
currentMode == 4
)
{
currentMode =
0;
}
else
{
currentMode =
4;
sweepRPM =
currentRPM;
sweepUp =
true;
lastSweepTime =
millis();
}
EEPROM.put(
11,
currentMode
);
}
}
// ============================================================
// 長押し
// ============================================================
void handleLongPress(
int btnNum
)
{
if (
btnNum >= 1 &&
btnNum <= 3
)
{
if (
currentMode == 0
)
{
presetRPM[
btnNum - 1
] =
currentRPM;
int addr =
5 +
(btnNum - 1) *
2;
EEPROM.put(
addr,
presetRPM[
btnNum - 1
]
);
lcd.setCursor(
0,
0
);
lcd.print(
"Preset "
);
lcd.print(
btnNum
);
lcd.print(
" Saved! "
);
delay(1000);
}
}
}
// ============================================================
// LCD表示
// ============================================================
void updateDisplay()
{
lcd.setCursor(
0,
0
);
switch (
currentMode
)
{
case 0:
lcd.print(
"Mode: Manual "
);
break;
case 1:
lcd.print(
"Mode: Preset 1 "
);
break;
case 2:
lcd.print(
"Mode: Preset 2 "
);
break;
case 3:
lcd.print(
"Mode: Preset 3 "
);
break;
case 4:
lcd.print(
"Mode: AutoSweep"
);
break;
}
// ----------------------------------------------------------
// PPR表示
// ----------------------------------------------------------
if (
pulsesPerRev ==
0.5
)
{
lcd.print("/");
}
else if (
pulsesPerRev ==
1.0
)
{
lcd.print("1");
}
else if (
pulsesPerRev ==
2.0
)
{
lcd.print("2");
}
else if (
pulsesPerRev ==
4.0
)
{
lcd.print("4");
}
else
{
lcd.print("?");
}
// ----------------------------------------------------------
// 2行目
// ----------------------------------------------------------
lcd.setCursor(
0,
1
);
char buf[17];
sprintf(
buf,
"%5drpm%4d.%1dHz",
currentRPM,
(int)currentFreq,
(int)(
currentFreq * 10
) % 10
);
lcd.print(
buf
);
}
// ============================================================
// 設定モード
// ============================================================
void settingsMode()
{
lcd.init();
lcd.backlight();
lcd.setCursor(
0,
0
);
lcd.print(
"Setup Mode"
);
EEPROM.get(
1,
pulsesPerRev
);
float pprOptions[] =
{
0.5,
1.0,
2.0,
4.0
};
int optionIndex =
1;
for (
int i = 0;
i < 4;
i++
)
{
if (
abs(
pulsesPerRev -
pprOptions[i]
) < 0.1
)
{
optionIndex =
i;
}
}
while (true)
{
int potValue =
analogRead(
potPin
);
int newIndex =
map(
potValue,
0,
1023,
0,
3
);
newIndex =
constrain(
newIndex,
0,
3
);
if (
newIndex !=
optionIndex
)
{
optionIndex =
newIndex;
lcd.setCursor(
0,
1
);
lcd.print(
"PPR(Pulse): "
);
lcd.print(
pprOptions[
optionIndex
],
1
);
lcd.print(
" "
);
}
// --------------------------------------------------------
// SW4長押し
// --------------------------------------------------------
if (
digitalRead(
sw4Pin
) == LOW
)
{
delay(1500);
if (
digitalRead(
sw4Pin
) == LOW
)
{
pulsesPerRev =
pprOptions[
optionIndex
];
EEPROM.put(
1,
pulsesPerRev
);
lcd.clear();
lcd.print(
"Settings Saved."
);
lcd.setCursor(
0,
1
);
lcd.print(
"Please Restart."
);
while (true)
{
}
}
}
}
}
一応、仕様上はそのように作ったつもりです。上の欄の16桁目が現在の1クランク当たりのパルス数になっています。/は0.5 あとは数字で表しています。
今のところ問題は見つかってませんがこちらは参考程度に使用してください。
装置のグランドレベルはタコメータと合わせておいてください。