Hi there! Completely new to Arduino.
I am building an optical tachometer using an IR sensor. The tacho works on an Arduino R3 board but I would like to use a pro-micro clone instead, the version I have is 5V 16MHz:
The code is based on the code from James Rovere's Easy Peasy Tachometer project (thanks very much) and works fine on an Arduino R3 using pin 2 as the sensor input. Here is the code:
// optical_tacho2 for Arduino R3
//delay turned to 1000ms
#include <Wire.h>
#include <Adafruit_SSD1306.h>// You may have to edit library for 128x64,
// default is 128 x 32.
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_ADDR 0x3C // A very common address for these displays.
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);
float value=0;
float rev=0;
int rpm;
int oldtime=0;
int time;
void isr() //interrupt service routine
{
rev++;
}
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
digitalWrite(2 ,HIGH);// Instead of using a pull up resistor. Use pin 2 on arduino R3 board
attachInterrupt(0,isr,RISING); //attaching the interrupt
}
void loop()
{
delay(1000);// 1 second delay
detachInterrupt(0); //detaches the interrupt while calculating
time=millis()-oldtime; //finds the time
rpm=(rev/time)*60000; //calculates rpm
oldtime=millis(); //saves the current time
rev=0;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);// Vertical, Horizontal.
display.println("RPM:");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,25);
display.println(rpm);
display.display();
attachInterrupt(0,isr,RISING);
}
For the Pro-micro clone I have changed the IR sensor input pin to 4:
/*
optical_tacho3_for cheap board.
Caution!! Use 5V 16MHz settings, else board gets bricked. Use the Arduino bookmarks folder to un-brick devices
This is the tacho for the cheap pro-micro clone
on COM22 (may change ports)
//delay turned to 1000ms
// digitalwrite pin changed to 4
*/
#include <Wire.h>
#include <Adafruit_SSD1306.h>// You may have to edit library for 128x64,
// default is 128 x 32.
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_ADDR 0x3C // A very common address for these displays.
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);
float value=0;
float rev=0;
int rpm;
int oldtime=0;
int time;
int sensor = 4;
void isr() //interrupt service routine
{
rev++;
}
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
digitalWrite(4 ,HIGH);// Instead of using a pull up resistor. Use pin 4 on clone
attachInterrupt(0,isr,RISING); //attaching the interrupt
}
void loop()
{
delay(1000);// 1 second delay
detachInterrupt(0); //detaches the interrupt while calculating
time=millis()-oldtime; //finds the time
rpm=(rev/time)*60000; //calculates rpm
oldtime=millis(); //saves the current time
rev=0;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);// Vertical, Horizontal.
display.println("RPM:");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,25);
display.println(rpm);
display.display();
attachInterrupt(0,isr,RISING);
}
However, when I upload the code to the pro-micro clone, I get a fixed value on the OLED of RPM: 57
Diagnosis so far:
- Works fine on R3 board with the IR sensor connected to pin 2
- Changing the Pro Micro board in IDE to Sparkfun Pro Micro, Adafruit 32U4 Breakout or Arduino Micro has no effect
- Changing delay(1000) to delay(2000) halves the number displayed to RPM: 29
- Changing attachInterrupt(0,isr,RISING); detachInterrupt(0); attachInterrupt(0,isr,RISING); to attachInterrupt(4,isr,RISING); detachInterrupt(4); attachInterrupt(4,isr,RISING); displays a fixed RPM: 0 on the OLED
Circuit diagram:
- Red wire is VCC
- Black wire is GND
- Orange wire is SDA (pin 2)
- Yellow Wire is SCL (pin 3)
- Brown wire is IR sensor input to board (pin 4)
Many thanks for any help