Ömüşiki
Published © MIT

Basic Arduino Uno PWM Signal Generator

Arduino PWM Signal generator with I2C LCD! There are 3 modes: Manual, Flasher and Breathing. Adjust speed / duty cycle with pot. Smart & com

IntermediateShowcase (no instructions)30 minutes4
Basic Arduino Uno PWM Signal Generator

Things used in this project

Story

Read more

Schematics

pwmsignalgenerator_e5A3R1Kx7X.PNG

Code

PwmSignalGenerator.ino

C/C++
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 16, 2); 

const int potPin = A0;      
const int buttonPin = 2;    
const int pwmOut = 9;       

int mod = 0;                
int buttonState;            
int lastButtonState = HIGH; 

void setup() {
  lcd.init();
  lcd.backlight();
  
  pinMode(buttonPin, INPUT_PULLUP); 
  pinMode(pwmOut, OUTPUT);
  

  lcd.setCursor(0, 0);
  lcd.print("MULTIMODE SIGNAL");
  lcd.setCursor(0, 1);
  lcd.print("    GENERATOR   ");
  delay(2500);
  lcd.clear();
}

void loop() {

  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {// 
    if (reading == LOW) { 
      mod++;
      if (mod > 2) mod = 0;
      lcd.clear();
      delay(200); 
    }
  }
  lastButtonState = reading;

  int potVal = analogRead(potPin);

  
  if (mod == 0) {
    int pwm = map(potVal, 0, 1023, 0, 255);
    analogWrite(pwmOut, pwm);
    
    lcd.setCursor(0, 0);
    lcd.print("MODE: MANUAL");
    lcd.setCursor(0, 1);
    lcd.print("POWER:%");
    lcd.print(map(pwm, 0, 255, 0, 100));
    lcd.print("    ");
  }


  else if (mod == 1) {
    int hiz = map(potVal, 0, 1023, 20, 500);
    digitalWrite(pwmOut, HIGH);
    delay(hiz);
    digitalWrite(pwmOut, LOW);
    delay(hiz);
    
    lcd.setCursor(0, 0);
    lcd.print("MOD: FLASHER");
    lcd.setCursor(0, 1);
    lcd.print("SPEED: ");
    lcd.print(hiz);
    lcd.print(" ms    ");
  }

else if (mod == 2) {
    static int light = 0;
    static int direction = 1;
    
    analogWrite(pwmOut, light);
    light += direction;
    
    if (light <= 0 || light >= 255) direction *= -1;
    
    int nefesHizi = map(potVal, 0, 1023, 1, 30);
    delay(nefesHizi);
    lcd.setCursor(0, 0);
    lcd.print("MODE: BREATHING ");
    lcd.setCursor(0, 1);
    lcd.print("AUTOMATIC MODE  ");
  }
}

Credits

Ömüşiki
2 projects • 0 followers

Comments