mdraber
Created February 16, 2021 © LGPL

Tutorial: Control 12V devices with Arduino Mosfet module

In this tutorial you will learn how to turn of and on 12V Led with the snap of the finger

BeginnerFull instructions provided398
Tutorial: Control 12V devices with Arduino Mosfet module

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Mosfet module
×1
Sound Sensor
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

mosfet_diagram_3nEHzFLoGJ.png

Code

Turning of 12V LED with the fingersnap

Arduino
int SoundDetected =0;
int sensorPin = 3;
int ledPin = 5;
int ledStatus=0;
void setup() {
   pinMode(sensorPin, INPUT);
   pinMode(ledPin, OUTPUT);
}

void loop() {
  SoundDetected=digitalRead(sensorPin);
  if (SoundDetected==1){
    if (ledStatus==0) ledStatus=1;
    else ledStatus=0;
    digitalWrite(ledPin,ledStatus);
    delay(100);
  }
}

Turning of 12V LED with the fingersnap (using interrupts)

Arduino
int ledPin = 5;
int ledStatus=0;
unsigned long Snap_Timestamp;

void setup() {
  attachInterrupt(1,Sound_Detected, RISING);
  pinMode(ledPin, OUTPUT);
}

void Sound_Detected() {
  if(millis()-Snap_Timestamp >200 ){
    if (ledStatus==0) ledStatus=1;
    else ledStatus=0;
    Snap_Timestamp=millis();
  }
}

void loop() {
  digitalWrite(ledPin,ledStatus);
}

Turning 12V LED on/off and controlling its brightness

Arduino
int ledPin = 5;
int ledStatus=0;
int brightness=0;
unsigned long Snap_Timestamp;

void setup() {
  attachInterrupt(1,Sound_Detected, RISING);
  pinMode(ledPin, OUTPUT);
}
void Sound_Detected() {
  if(millis()-Snap_Timestamp >200 ){ 
    if (ledStatus==0){
       ledStatus=1; brightness=1;
    }
    else {
      if(millis()-Snap_Timestamp <1000 ) brightness++;
      if(brightness>5)brightness=5;
      if(millis()-Snap_Timestamp >=1000) {
        ledstatus=0;
        brightness=0
      }
    }
    Snap_Timestamp=millis();
  }
}
void loop() {
  analogWrite(ledPin,brightness*50);
}

Credits

mdraber
50 projects • 74 followers
Contact

Comments

Please log in or sign up to comment.