Basically Off But You can hold On




So, I did a simple code that I thought would be fun to share. Its more or less like the basics of embedded systems. Inputs and Outputs. Actually, maybe its the basics of everything.

So I'm Kenyan and our currency is the Shilling(Ksh) hence any time i mention money, I will refer to it in Ksh form.
You will approximately need about Ksh 800 to have this project up and running(FYI, everything you use in this project is reusable and integrate-able to other projects).

Requirements:
A push button
An LED
Two resistors( I used 220 Ohm and a 10K one)
A Microcontroller (MCU) I will be using node MCU but you can do it using Arduino with exactly the same code that I made.
A lot of jumper wires of course.


I made a breadboard layout of how you can place your parts. I made it using Fritzing. Feel free to download and start drawing out your projects.

The logic of the code is that: Before the button is pressed, the LED is off. When the button is being pressed, the LED is on and when you release it, it goes off once again.

The code is pretty basic.

int led = 13; //D7(node mcu)  use pin 12 if using arduino

int button = 12; //D6(node mcu) use pin 12 if using arduino
int buttonState = 0;

void setup() {
  Serial.begin(115200); // use 9600 if using arduino
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  buttonState = digitalRead(button);

  if (buttonState == 1)
  {
  digitalWrite(led,HIGH);
  Serial.println("ON");
  delay(500);
  }
  
  if (buttonState == 0)
  {
  digitalWrite(led,LOW);
  Serial.println("OFF");
  delay(500);
  }
  
}


Things to note while doing this project:
1. The LED legs. The short one is grounded and the long one is connected to a resistor(220Ohms) and the resistor connected to one of the pins on the board. (as for our case pin 12)

2. The legs of the push button are paired and when you connect one pair to ground and the other to live, then you complete the circuit. You can use a multi-meter to figure this out in case you get problems with it.

3. Its pretty basic but if you are not keen you may end up taking a lot of time doing it or even blowing up your stuff or sthn.

Have fun!



Comments

Popular posts from this blog

MIT App Inventor

Tech ventures: The Bluetooth module(HC-05 zs-040)

The LoadCell Project