/* Sweep by BARRAGAN This example code is in the public domain. modified 8 Nov 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Sweep */ #include Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
This is a very simple program which makes the motor rotate from 0 to 180 degrees by 1 degree step and then come back to 0 with the same step.
Here you can find the file with the code and the schematic file.
Ultrasonic distance sensors use pulses of ultrasonic sound (sound above the range of human hearing) to detect the distance between them and nearby solid objects.It determines the distance to an object by measuring the time taken by the sound to reflect back from that object. The sensors consist of two main components:
– An Ultrasonic Transmitter – This transmits the ultrasonic sound pulses, it operates at 40 KHz
– An Ultrasonic Receiver – The receiver listens for the transmitted pulses. If it receives them it produces an output pulse whose width can be used to determine the distance the pulse travelled.
We have to consider that the speed of sound in air varies with temperature, air pressure and humidity. Since the speed of sound factors into our distance calculation this can affect to get presized readings if there are temperature and humidity changes. At the same time ultrasonics are Independent of light, smoke, dust, color.
To make life easier when working with ultrasonic distance sensors and Arduino, you can install the UltraDistSensor library. For that go to Sketch → Include library → Manage libraries, search and find UltraDistSensor and install it.
The LCD1602 display module is a very popular and inexpensive LCD display. It consists of two rows of 16 characters each. It interfaces through a parallel data interface and has an LED backlight. My LCD has an I2C interface module mounted on the back. With this I2C module, you only need two connections to control the LCD.
Arduino has the LiquidCystal_I2C library, which makes working with LCD displays very easy and enjoyable. To install that library, download the .zip file from the link above, go to Sketch → Include library → Add ZIP libraries and upload the .zip file.
Piezo transducer uses a material that’s piezoelectric, it actually changes shape when you apply electricity to it. By adhering a piezo-electric disc to a thin metal plate, and then applying electricity, we can bend the metal back and forth, which in turn creates noise. The faster you bend the material, the higher the pitch of the noise that’s produced. This rate is called frequency. Again, the higher the frequency, the higher the pitch of the noise we hear.
It has a very simple connection to the Arduino. One of the two wires (-) is connecting to the Ground and the second one (+) to one of the digital pins.
Arduino has the tone() function which generates a square wave of the specified frequency to the specifed pin. You can also give an argument for the duration of the tone, otherwise the wave continues until a call to noTone().
Now let’s see how to connect everything together and make it work.
The connections are done. Now connect Arduino to the computer and upload the following code.
#include UltraDistSensor.h #include Wire.h #include LiquidCrystal_I2C.h LiquidCrystal_I2C lcd(0x27, 16, 2); UltraDistSensor mysensor; float reading; int piezoPin = 8; void setup() { lcd.init(); // initialize the lcd lcd.backlight(); Serial.begin(9600); mysensor.attach(12,13);//Trigger pin , Echo pin } void loop() { reading = mysensor.distanceInCm(); if (reading >= 400 || reading <= 2) { lcd.clear(); lcd.setCursor(0,0); lcd.print("Out of range"); Serial.print("Out of range"); } else { lcd.setCursor(0,0); lcd.print("Distance is "); lcd.setCursor(0,1); lcd.print(" "); lcd.setCursor(0,1); lcd.print(reading); lcd.print("cm"); Serial.print(reading); } delay(100); if (reading <=30) { tone(piezoPin, 1000, 2000); }; }
Here is the result:
Here you can find the file with the code and the schematic file for the distance sensor.
make -f hello.RGB.45.make
make -f hello.RGB.45.make program-usbtiny
I added all of the components from the KiCad library and started to connect them.
Then I designed the PCB, exported it as svg and cut the board as it’s described in Electronics production and electronics design weeks. I checked all of the connections by the digital multimeter, seems everything is fine.
Programming was successful but the board didn’t work. Then I started to check the connections again, all was fine. Afterwards I have checked the footprints and found out that I have a wrong footprint and connections for the voltage regulator. So I have changed the footprint and connections to the proper ones. This time I moved the components closer to each other and my board got smaller.
After assembling the board, I did the programming and this time it worked, hooray.
hello.RBG.45 original design files
File with the code and the schematic file for the distance sensor.
file with the code and the schematic file for servo motor.
February 3, 2022