Arduino
For this practical, I have been tasked to create some input and output systems. Below in this blog, I will demonstrate and showcase the steps and code to create this devices.
1a.
The first device is to be a device that can read the signal from a potentiometer and display it on the serial monitor. To start off, we need to connect the "physical" components.
Its just a potentiometer wired to the arduino board. The outer pins are ground and voltage, and the middle pin will be the input for the system, which we will connect to the Analog pin A0.
Next is the code; start by defining the input variable which we will call "sensor". Then we type in the void setup, use pinMode to set the analog pin A0 as an input.
Now is the void loop. We will link the variable "sensor" to the signal of the potentiometer which is connected to pin A0. Then we print the variable "sensor" in the serial monitor, and the sensorValue which is the input pin A0 and the signal of the potentiometer. We then add a delay so that the potentiometer signal is only displayed every 10ms and doesn't clog the monitor.
Here's the link for this system on TinkerCad:
1b.
The second input system uses a photoresistor instead of a potentiometer. The luminosity will change the resistivity of the resistor which will be the input signal of this circuit.
Our circuit has a photoresistor connected to power and a resistor to ground; a wire after the resistor goes the analog pin A0 which would capture the signal of the photoresistor to be the input.
Apart from the 10k resistor and the photoresistor only having 2 legs instead of the 3 of the potentiometer, the circuitry of these two systems are very similar; the code is identical so I won't go into detail on it.
The TinkerCad link for this photoresistor circuit is here:
This next task is to interface 3 LEDs of colors red, yellow and green to the board and do something. These colors are the same as a traffic light so I will create a traffic light system with a shorter time period.
The breadboard setup is simple, the power supply for the LED will come from the Arduino board output pins. Connect a pin to the anode of each LED, then connect a 1k resistor to the cathode to ground.
Onto the code, first step is to tell the board which pins are connected to the LEDs. So we use pinMode in void setup and set pins 13,12 and 11 to be the output pins.
Since it is a traffic light, we want the lights to go from red to yellow then green sequentially. There should only be one light on at a time, and when one light turns off the next one turns on.
Use digitalWrite to set the red light(pin 13) to HIGH which will turn it on; then let it be on for a second before setting it to LOW and type digitalWrite for the yellow light(pin 12) to HIGH. Repeat for yellow to green and green to yellow and we're done.

This last output system is to combine a push button with a DC motor; the push button being the switch that turns the motor on.
This system is quite complex comparatively to the others in terms of the physical setup. We have an NPN BJT transistor, a diode and a motor. Motors can't handle sudden changes in direction of current, so we need a diode to ensure that current flows in one direction. You can think of it as a check valve to a centrifugal pump, with the DC motor being the pump. Transistors allow current to flow only when the base pin receives current, this allows it to function as a switch that works by electricity. We will use this transistor to receive the output of our arduino board and turn the motor on. Lastly we just have a push button that will be connected to our input pin 2 that will be the input of the system.
We want the motor to turn on for a few seconds then turn itself off. For this we need to code in a void loop using the push button as an input and the motor pin 11 as an output. As usual we setup the pins we are working with using pinMode to input or output in the void setup, as well as define the variable of the button state.
Next we link the button state to the signal of the input pin which is the push button. Then we set up an if or logic, where if the button is pushed the signal would be high and a void loop action would happen, or the motor pin would remain low. When the button is pushed, we want the motor to be on so digitalWrite(11, HIGH). We want the motor to be on for a bit, so a delay of 5000ms, then set the pin to LOW.
Link for the push button motor is here:
Comments
Post a Comment