English
EspaƱol
yt_link
insta_link
fb_link
twitter_link
ADVERTISERS
PCBWAY PCB Manufacturing

PCBWAY Christas PCB Manufacturing

Advanced PCB Manufacturing
Advanced PCB Manufacturing





AFFILATE










Gearbest Creality3D CR - 10S 3D Printer - EU Plug Upgrade Version Coffee and Black
Creality3D CR - 10S 3D Printer - EU Plug Upgrade Version Coffee and Black only $$352.94

Gearbest Creality 3D Ender-3 V-slot Prusa I3 DIY 3D Printer Kit 220 x 220 x 250mm Printing Size - EU Plug Black
Creality 3D Ender-3 V-slot Prusa I3 DIY 3D Printer Kit 220 x 220 x 250mm Printing Size - EU Plug Black only $$153.84

Gearbest Alfawise U20 Large Scale 2.8 inch Touch Screen DIY 3D Printer - EU - U20 EU Plug Black
Alfawise U20 Large Scale 2.8 inch Touch Screen DIY 3D Printer - EU - U20 EU Plug Black only $$253.39


Arduino based RPM meter (3D printed case)




Libraries

Now that we have the case we have to program the Arduino. For that you first need to download the next two libraries for the OLED screen. Download them from the next link or install them using the library manager in the Arduino IDE.

Download the Adafruit_GFX.h library here:
Download the Adafruit_SSD1306.h library here:


Or you could just install the libraries using the Arduino IDE. For that go to Sketch, include library, library managesr and search for your desired library. Once found, select the version and click install just as in the picture below.

install Arduino library





The code

The code is simple. When the push button is pressed, the meter is activated. We know that the LM324 will give us a low pulse each time the white stripe pases in front of the sensor representing one full rotation. We have to start a counter when we detect the first positive edge representing the final of the white stripe and count the time till we detect another positive pulse, representing the start of the white stripe again. We make the difference of the measure counters and we obtain the time for one rotation in microseconds. We divide one minute (60.000.000us) by that value and we obtain the rotations per minute. We print the value to the OLED screen uisng the text function.

When the push button is released the Arduino will be in low power mode. You could turn it off with the slide switch as well.

Download the full code here:


//For low power mode
#include <avr/sleep.h>
#include <avr/power.h>

//OLED libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);



First we include all the libraries. Remember to download the liblaries above. Now that we have the libraries we define the vareiables and start the Setup loop. In the Setup loop we start the OLED screen, define the pins as inputs and print the electronoobs LOGO.

Be careful, the i2c adress in my case is 0x3C in the display.begin function in the Setup void. If you don't knwo the i2c adress of your module use the next code to find it. Download the sketch. Upload it to your arduino. Connect the SCD and SDA pins and open the Arduino monitor. Select 9600 baudrate. The i2c adress will be printed on the monitor.

Download the i2c scanner code here:




//In and Out
int in = 13;
int pushbutton=9;
//Variables
unsigned long duration = 0;
float rpm = 0;
float rpm_a = 0;
int counter = 0;
int present = 0;
int previous = 0;
unsigned long elapsed = 0;
unsigned long elapsed_prev = 0;
int disabled = 0;





void setup()   {                
  Serial.begin(9600);
  pinMode(in,INPUT);
  pinMode(pushbutton,INPUT);
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done  
  // Clear the buffer.
  display.clearDisplay();

  // text display tests
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("ELECTRONOOBS RPMmeter");
  display.display();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,19);
  display.println("RPM:");
  display.setCursor(80,19);
  display.println(rpm);  
  display.display();

  //We print the ELECTRONOOBS logo
  scrollENlogo();
  
  
  elapsed = micros();
}


Now in the loop we check if the push button is pressed. If yes we measure the elapsed time for each rotation. Next we calculate the rotation per minute and print the value to the OLED screen. If the button is not presed we put the Arduino in low power mode.





void loop() {


if(digitalRead(pushbutton))
{

  //Arduino low power enabled
  if(disabled==0)
  {
    sleep_disable();
    disabled = 1;
  }
///////////////////////one rotation measure///////////////////
   if (digitalRead(in) == 1 && previous == 0)
  {
    previous = 1;
    duration = elapsed - elapsed_prev;
    elapsed_prev  = micros();    
  }
  if (digitalRead(in) == 1 && previous == 1)
  {
    previous = 1;       
  }
  if (digitalRead(in) == 0 && previous == 1)
  {
    previous = 0;     
  }
  if (digitalRead(in) == 0 && previous == 0)
  {
    previous = 0;
    elapsed = micros();    
  }
//////////////////////////////////////////////////////////////
   
   
   
   rpm = 60000000/duration;


//We add a small error in the rpm value (in this case +-2)
if ( (rpm_a-2) < rpm  &&  rpm < (rpm_a+2))
{
  rpm_a = rpm;
  counter = counter +1;
  if (counter == 50)
  {
     // text display tests
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println("ELECTRONOOBS RPMmeter");  
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0,19);
    display.println("RPM:");
    display.setCursor(80,19);
    display.println(rpm);  
    display.display();
    counter = 0;
  }
}

if (!( (rpm_a-2) < rpm  &&  rpm < (rpm_a+2)))
{
  rpm_a=rpm;
}
}//end if pushbutton=1



else{
  //Variables
  display.display();
  display.clearDisplay();
  delay(10);
  duration = 0;
  rpm = 0;
  rpm_a = 0;
  counter = 0;
  present = 0;
  previous = 0;
  
  //Arduino low power enabled
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
  disabled = 0;
}//end pushbutton =0
}//end of void loop








void scrollENlogo(void) {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(7,0);
  display.clearDisplay();
  display.println("ELECTRONOOBS");
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,10);
  display.println("WELCOME");
  display.display();
  delay(1);
 
  display.startscrolldiagright(0x00, 0x07);
  delay(5000);
  display.stopscroll();
  
}