Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* REQUIRES the following Arduino libraries:
- - Adafruit GFX graphics core lib: https://github.com/adafruit/Adafruit-GFX-Library
- - Adafruit OLED mono displays lib: libray https://github.com/adafruit/Adafruit_SSD1306
- tutos et références
- https://www.carnetdumaker.net/articles/faire-un-tachymetre-avec-une-carte-arduino-genuino-pour-lire-la-vitesse-de-rotation-dun-ventilateur-de-pc/
- brochage Ventirad4 : GND, VCC, Tachymeter, PWM
- PWM on : 3 5 6 9 10 11 https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm/
- https://passionelectronique.fr/pwm-arduino/
- PWM 976.56 Hz D5 D6
- PWM 490.20 Hz D3 D9 D10 D11
- Arduino Nano
- display SSD1306 128*32
- potentiomètre 10kOhm
- résistance 10kOhm
- diode 1N4007 ou 1N4148 peut être nécessaire sur l'entrée D2, selon le type de ventilateur
- régulateur de tension LM2596D peut être nécessaire, selon le type d'alimentation de l'arduino nano
- VentiRAD 4 fils (CPU cooler) : FOXCONN PUA080G12Q 900-4600 RPM
- données à ajuster selon le VentiRAD
- une régulation interne le fait tourner au ralenti quand PWM est remis à 0. On pourrait ajouter un relais.
- */
- #include "time.h"
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <Wire.h>
- // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
- // The pins for I2C are defined by the Wire-library.
- // On an arduino UNO or Nano: A4(SDA), A5(SCL)
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 32 // OLED display height, in pixels
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- #define SCREEN_ADDRESS 0x3C //< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- #define Tach_detection_pin 2 // digital inp
- #define analogInPin A0 // Analog input pin that the potentiometer is attached to
- #define PWMOutputPin 5
- #define HDD_MAXRPM 4600 // à modifier selon le ventilateur
- #define baudrateSerial 115200 // vitesse communication Arduino - PC
- volatile float Tach_time;
- volatile float Tach_last_time = 0;
- unsigned long start_time4min, start_time4sec;
- unsigned int plannedRPM, sentRPM;
- unsigned long realRPM;
- bool bDisplay = false;
- // déclarations anticipées de fonctions
- // void(* resetFunc) (void) = 0; // déclaration de la fonction interne Reset de l'arduino
- bool SSD_init(void);
- void SSD_display(void);
- void setup() {
- // initialize Tach pins
- pinMode(Tach_detection_pin, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(Tach_detection_pin), onTach_Event, FALLING); // RISING FALLING
- analogReference(DEFAULT); //(EXTERNAL);
- pinMode(analogInPin,INPUT);
- // analogWriteResolution();
- pinMode(PWMOutputPin, OUTPUT);
- bDisplay = SSD_init();
- //Serial.begin(baudrateSerial);
- }
- void onTach_Event() {
- unsigned long _Time = micros();
- Tach_time = _Time - Tach_last_time;
- Tach_last_time = _Time;
- }
- void loop(){
- unsigned int sensorValue = 0;
- for (int i = 0; i < 16; i++){
- // Read light sensor data.
- sensorValue = sensorValue + analogRead(analogInPin);
- // 1ms pause adds more stability between reads.
- delay(5);
- }
- // Take an average of all the readings.
- sensorValue = sensorValue / 16;
- plannedRPM = map(sensorValue, 0, 1023, 0, HDD_MAXRPM * 1.005); // 1.005 car tolérance dans le nombre de RPM :-)
- sentRPM = map(sensorValue, 0, 1023, 0, 255);
- analogWrite(PWMOutputPin, sentRPM);
- if (Tach_time > 0){
- realRPM = 60 * 1000000.0 / Tach_time / 2 ; // diviseur à modifier selon le ventilateur
- }
- if (bDisplay) SSD_display();
- delay(200);
- }
- bool SSD_init(void){
- // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Don't proceed, loop forever
- }
- // Show initial display buffer contents on the screen --
- // the library initializes this with an Adafruit splash screen.
- display.display();
- display.cp437(true); // Use full 256 char 'Code Page 437' font
- display.setTextColor(SSD1306_WHITE); // Draw white text
- delay(1000);
- return true;
- }
- void SSD_display(){
- static char buffer[18];
- display.setTextSize(1); // Normal 1:1 pixel scale
- display.clearDisplay(); // Clear the buffer
- sprintf(buffer,"Set RPM : %4d", plannedRPM);
- display.setCursor(0,0); // Start at left-top corner
- display.print(buffer);
- sprintf(buffer,"real RPM: %4d", realRPM);
- display.setCursor(0,20);
- display.print(buffer);
- display.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement