From e59aae650f3bafe112489c0a2955a924a9d84d8f Mon Sep 17 00:00:00 2001 From: Pawel Koscielny Date: Thu, 11 Mar 2021 16:17:33 +0100 Subject: [PATCH] Added fully working code --- README.md | 16 +++++- air_throttle.ino | 67 ++++++++++++++++++++++++ air_throttle_with_oled.ino | 103 +++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 air_throttle.ino create mode 100644 air_throttle_with_oled.ino diff --git a/README.md b/README.md index 82e1d93..2210da2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,14 @@ -# air_throttle -Air flow controlled by throttle with stepper motor +# Air throttle +Air flow controlled by throttle valve with stepper motor. +A Hall-effect sensor detects when a stove is running and open the throttle. +Otherwise the throttle is closed. + +## Hardware + +### List of components +* SparkFun Pro Micro 5V 16MHz +* stepper motor 28BYJ-48 +* ULN2003 driver for stepper motors +* Hall-effect sensor ACS712 - 20A +* simple ventilation throttle valve +* 0.96 OLED display (optionally) diff --git a/air_throttle.ino b/air_throttle.ino new file mode 100644 index 0000000..d687a9c --- /dev/null +++ b/air_throttle.ino @@ -0,0 +1,67 @@ +/* + * Hardware: + * - board SparkFun Pro Micro 5V 16MHz. + * - step motor 28BYJ-48 with ULN2003 driver + * - hall sensor ACS712 - 20A. + * +*/ + +#include +#include + +// Motor. +const int steps = 32; // the number of motor steps. +const int rotate_360 = 2048; // 32*64 - because of gear. +Stepper motor(steps, 6, 8, 7, 9); // pay attention to pins order. Current order is proper. + +// HALL effect sensor. +// SparFun has 5.0 volt with a max value of 1023 steps. +// ACS712 5A uses 185 mV per A. +// ACS712 20A uses 100 mV per A. <<----------- +// ACS712 30A uses 66 mV per A. +ACS712 acs(A0, 5.0, 1023, 100); + + +void setup() { + // Init rotor. + motor.setSpeed(800); + + // Init HALL sensor. + acs.autoMidPoint(); +} + + +bool is_stove_running = false; +bool is_throttle_opened = false; +int mA; // the value taken from Hall sensor (in miliampers). + +void loop() { + // Read current. + mA = acs.mA_AC(); + //Serial.println(mA); + + // Stove is running. + is_stove_running = (mA > 200) ? true : false; + + // Run motor if needed. + if (is_stove_running && !is_throttle_opened) { + open_throttle(motor); + is_throttle_opened = true; + } + else if (!is_stove_running && is_throttle_opened) { + close_throttle(motor); + is_throttle_opened = false; + } + + delay(500); +} + + +void open_throttle (Stepper motor) { + motor.step(rotate_360/4); // 512 == turn 90 deg. +} + +void close_throttle (Stepper motor) { + motor.step(-rotate_360/4); // 512 == turn 90 deg. +} + diff --git a/air_throttle_with_oled.ino b/air_throttle_with_oled.ino new file mode 100644 index 0000000..baf7006 --- /dev/null +++ b/air_throttle_with_oled.ino @@ -0,0 +1,103 @@ +#include +#include +#include +#include + +// OLED display. +#define OLED_ADDR 0x3C +Adafruit_SSD1306 display(-1); + +// Motor. +const int steps = 32; // the number of motor steps. +const int rotate_360 = 2048; // 32*64 - because of gear. +Stepper motor(steps, 6, 8, 7, 9); // pay attention to pins order. Current order is proper. + +// HALL effect sensor. +// SparFun has 5.0 volt with a max value of 1023 steps. +// ACS712 5A uses 185 mV per A. +// ACS712 20A uses 100 mV per A. <<----------- +// ACS712 30A uses 66 mV per A. +ACS712 acs(A0, 5.0, 1023, 100); + + +void setup() { + // Init rotor. + motor.setSpeed(800); + + // Init HALL sensor. + //pinMode(A0, INPUT); + acs.autoMidPoint(); + + // Init and clear OLED display. + display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); + display.clearDisplay(); + display.display(); + display.setTextColor(WHITE); +} + + +bool is_stove_running = false; +bool is_throttle_opened = false; +int mA; // the value taken from Hall sensor (in miliampers). + +void loop() { + // Read current. + mA = acs.mA_AC(); + display_hall(mA, is_throttle_opened); + //Serial.println(mA); + + // Stove is running. + is_stove_running = (mA > 200) ? true : false; + + // Run motor if needed. + if (is_stove_running && !is_throttle_opened) { + display_throttle_state("opening..."); + open_throttle(motor); + is_throttle_opened = true; + } + else if (!is_stove_running && is_throttle_opened) { + display_throttle_state("closing..."); + close_throttle(motor); + is_throttle_opened = false; + } + + delay(500); +} + +void open_throttle (Stepper motor) { + motor.step(rotate_360/4); // 512 == turn 90 deg. +} + +void close_throttle (Stepper motor) { + motor.step(-rotate_360/4); // 512 == turn 90 deg. +} + +// Views definitions. +void display_hall(int hall, bool is_throttle_opened) { + display.clearDisplay(); + display.setTextSize(1); + display.setCursor(0, 0); + display.print("Hall [mA]:\n"); + display.setTextSize(2); + display.print(hall); + display.print("\n"); + display.setTextSize(1); + display.print("Throttle: "); + if (is_throttle_opened) { + display.print("opened"); + } else { + display.print("closed"); + } + display.display(); +} + +void display_throttle_state(String msg) { + display.clearDisplay(); + display.setTextSize(1); + display.setCursor(0, 0); + display.print("Throttle:\n"); + display.setTextSize(2); + display.print(msg); + display.display(); +} +