This week's assignment is to build and program a smart circuit using an Arduino UNO which reads a signal from an input component to control an action component. I tried to implement the minimum features of my final project which is a Automatic page turner which utilizes a wheel and an arm to flip pages without using hands only a push of a button!
Software: Tinkercad
Software:Arduino IDE
Components: 5v dc motor
Components: Arduino Uno board
Components: L298N DC motor driver module
Components: Micro Servo motor
Components: Jumper wires
Components: Screw Terminal block to female DC power adapter
Components: 9V adapter
Components: Breadboard
Components: Push button
Circuit connection on Tinkercad
Circuit connection on Fritzing (exactly as the physical)
Circuit design:
Link for circuit: Here
On Tinkercad, I started to put the components I will use in my circuit.
I put the Arduino board and breadboard.
Mainly, the circuit consists of two action components (OUTPUT) which are the dc motor and the servo motor, so I added them.
A push button is also added as an input signal for the circuit (INPUT).
I used the L2930 motor driver in simulation because the L298N doesn't exist on Tinkercad.
I used a 9v adaptor to power the dc and arduino 5v for servo.
I connected 5V and GND in arduino with +ve and -ve rails on the breadboard in both sides.
I connected the Servo on analog output pin 10 for signal and powered it using the rails.
Then, I connected the dc motor with the driver after reviewing the pinout from midweek tutorials. It is connected with arduino using 3 pins, 2 (digital) for direction control and 1(analog) for speed control.
I connected the driver pins on 6(analog), 7, 8 [digital].
For the L2930 in tinkercad I connected it with power supply (9v) and also with arduion 5v and grounded it to both arduino GND and power supply GND and motor terminals.
For the L298N in physical circuit and fritzing, it was a little bit different. I connected the motor terminals, the power terminals (9v) and GND arduino with GND power in the same place and finally the 3 contorl pins.
Finally, the push button is connected on pin 2 to trigger motors' motion. Because I couldn't do the INPUT_PULLUP mode using blocks, I edited the connection to perform the same funciton.
Using examples as guide to write the button state detection
Code blocks of the code
Code & logic:
I started first by dividing the problem and defining the objectives clearly and what I want to achieve.
As I want to make the push button trigger an action every time it is pressed and not only when holded down, I searched how to do so and I found several ways but the state change detection example was the easiest one to edit.
It is simply done by using two variables, current button state and previous button state. The current one stores the readings from the button pin and at the end of the void loop the value is stored in the previous button state variable.
By comparing the values of these variables, I can simply detect the current state of the button if it is pressed or not.
I thought what is the right condition I should write and decided to make it like the following: If (btnState is HIGH and prevBtnState is LOW) => triggers the motors motion.
This means when the button is pressed and before that it wasn't pressed. This technique is used mainly to prevent the repetition of the action while the button is held down.
After that, I put the motor blocks. The dc moves with speed equal to the value 180 and in the positive direction for about 1.2 sec then it stops by giving it value 0 as speed.
Then Servo moves 180 degrees and go back to 0 to flip the page.
Simulation on Tinkercad to test the circuit
Circuit Implementation:
I started putting the components on the breadboard.
I connected the 5v and GND from Arduino to the rails on breadboard.
I connected the servo with arduino power and signal pin
I connected the L298N motor driver module with motor and connected its pins with arduino
Then I connected the push button along with the circuit.
I copied the code from tinkercad and put it in arduino IDE to be able to upload it to the arduino.
I changed pinMode for the push button to be INPUT_PULLUP and accordingly changed the condition to be LOW for current button state and HIGH for previous button state
Finally, I selected the board and the port then clicked upload.
5v and gnd from Arduino
Servo connection with breadboard and pin 10
9V and GND from power adapter to driver power terminals and yellow wire to GND of arduino
Motor terminals connected to output 1 and output 2
3 Control pins, enable pin (speed) and two IN pins for direction (6, 7, 8 pins)
I selected blocks + text and copied the code from tinkercad
I pasted it in Arduino IDE and selected the board to Arduion UNO and the port which is associated with arduino USB and clicked the Upload which is the button with arrow on top left
Final outcome
After building the circuit in tinkercad I thought everything is fine and it will work on the physcial circuit but an unexpected thing happend. When I clicked the button, only the servo worked after a time interval (which was specified for the dc to work). It seemed like the arduino thinks it is working but it is not. After excluding the power to be the problem because each motor has different power source, I started to debug by testing each component on its own and it worked fine. I noticed that the dc motor doesn't work when I put the servo code with it. I searched for the problem and found someone who encountered the same problem and someone explained it as a pin problem. In this thread on arduino forum, someone said it is a problem in the servo library because it uses a timer and it utilizes pins 9 and 10, so if you are connecting analog pins in those along with servo, a conflict will happen. I simply changed the pin to 6 instead of 9 and it finally worked!
The response I found helpful from mancera1979
I reflected what I learned to achieve the minimum features for my final projects utilizing what learned with Tinkercad and Arduino IDE
It was very cool when I realized that I can make a custom soundbite with buzzer in the hands on minds on activity, we made happy birthday soundbite and For elise and it was nice!
This week we controlled many new components and used different sensors like ultrasonic. and made nice waving hand with cardboard enclosure.