Tuesday, 13 October 2015

How to make sound with soil moisture sensor


What bettar way of knowing that your plant or garden wants water by giving you sound alert! Well, in this post I'll share a sketch which exactly does that. It make a sound alert when it needs water plus turns on a led to give you a visual alert too.

Let's see the circuit now.

And the sketch.
int led1 = 4;

void setup() {
// initialize serial communication at 9600 bits per second:
pinMode(led1, OUTPUT);
Serial.begin(9600);
}


void beep1(){

  tone(8, 440, 200);
  delay(50);
  noTone(8);
  delay(100);
 
  tone(8, 440, 200);
  delay(100);
  noTone(8);
  delay(100);
 
  tone(8, 440, 200);
  delay(200);
  noTone(8);
  delay(100);
 
}

void loop() {

int sensorValue = analogRead(A2);

if (sensorValue >=300 && sensorValue <=800){
Serial.println(sensorValue);
    beep1();
    digitalWrite(led1, HIGH);
  }
  else {
Serial.println(sensorValue);
    digitalWrite(led1, LOW);
  }
delay(300000);
// reads every five minutes
}

I've already talked about led, analog pin, and value in detail on my other post, if you need help on that then please see that post. Over here I've just added one extra function and that's Arduino's built in tone function. To make the sound you won't need any extra hardware, you just need few lines of code and that is

tone(8, 440, 200);
delay(50);
noTone(8);

  • The number 8 is in the code is for buzzer or speaker, remember if you change the pin number in one place change the number from other place too. Both should be the same number, otherwise your speaker won't work. 
  • 440 and 200 is to control the pitch of the sound. It will make different sound if you change the numbers up and downd.
  • delay(50); This number is to make the sound shorter or longer.
That's all for this post. Thanks :)

1 comment:

  1. This framework offers a complete sound arrangement with the upside of a remote subwoofer and forefront sound innovations. Vizio s5430w-c2

    ReplyDelete