Test of the Electronics Project Page
I created this project as a test of the electronics page. I am trying to get back into my electronics hobby, and so I followed some tutorials to remind myself of some concepts for a couple hours today.
Why Create This
I wanted to create this project to get back into my electronics hobby. As you can read on the electronics page, I first got into electronics / coding microcontrollers after realizing that my mechanical engineering coursework would not provide me with enough skill to build something sufficiently complex. I was interested in robotics so I ended up taking a mechatronics class during my undergraduate degree, and in this class, we coded a PIC microcontroller using BASIC to control a stepper motor and LCD screen based on inputs from a numerical keypad. Learning from the difficulty of this experience, which mainly arose due to the dearth of BASIC code to be found for controlling PICs, I decided to look into other microcontrollers. This is when I found out about Arduino and Raspberry Pis.
I created this project because it has been a while since I had really done much electronics related. Somewhat recently, I had replaced the motherboard on my 3D printer and added a BL Touch bed leveling sensor to my Creality Ender 3, but projects like these are more so following directions and just making sure that everything was put together correctly versus actually coming up with an idea, coding it, and designing the electrical system yourself. To get back into the electronics hobby, I worked with the Arduino some today. The result of me working with it can be seen below. I would have created something more complex, but I needed to move on to do something else, so I submitted what I had to this site (just to have at least one project on the site at all times).
The Project
A demonstration of the small project can be seen below. The project just outputs random colors to two RGB LEDs, controls a red LED with two switches, and plays some music on a active buzzer. The code for the project can be seen below.
RGB LED
An RGB LED contains three LEDs: one red, one green, and one blue. By supplying the three LEDs with different currents, you can change the strength of the light of each LED, and by changing the strength of the light of LED, you can create whatever color you want to. Using pulse with modulation, which you can use with Analog outputs on the Arduino board, you can control the voltage across the LEDs and the current that goes to each one. The image to the right shows an RGB LED.
Pulse With Modulation
PWM is a technique for controlling power. The diagram to the left demonstrates pulse with modulation. Digital signals can only be 0 or 1 (0V or 5V), so to output a voltage between 0V and 5V, the computer / microcontroller can control how much time it is high (5V) vs low (0V). By alternating between 5V and 0V quickly and by controlling the amount of time the output pin is high vs low, the microcontroller can output an effective voltage between 0V and 5V.
Color
Your eye has three types of light receptor in it (red, green, and blue). By outputting certain amounts of red light, green light, and blue light from an RGB LED, you can effectively trick your eye into seeing a mix of those three colors, e.g. pink.
Switches
When you press a switch or flip a lever, these switch components connect two contacts together so that electricity can flow through them. Note: This is only for normally open switches. Using this principle and the ability to read voltage using an Arduino (see digitalRead
in the code below), you can control components based on switch behavior.
Active Buzzer
Electronic buzzers are DC-powered and equipped with an integrated circuit. They are widely used in computers, printers, photocopiers, alarms, electronic toys, automotive electronic devices, telephone, timers, and other electronic products for voice devices.
Buzzers can be categorized as active and passive buzzers. The difference between the two is that an active buzzer has a built-in oscillating source, so it will generate a sound when electrified. A passive buzzer does not have such a source so it will not make a sound if DC signals are used - you have to use square waves with frequency between 2K and 5K to drive them,
Code
Explanation of Some Arduino Functions and Constants Below
The Arduino language reference can be found on this page.
pinMode
- Configures the pin to behave either as an
INPUT
orOUTPUT
- Configures the pin to behave either as an
A#
- A reference to an analog pin on the board
OUTPUT
- Pin used as output
INPUT_PULLUP
- The ATmega microcontroller on the Arduino has internal pull-up resistors (resistors that connect to power internally) that you can access. If you prefer to use these instead of external pull-up resistors, you can use the
INPUT_PULLUP
argument inpinMode
- The ATmega microcontroller on the Arduino has internal pull-up resistors (resistors that connect to power internally) that you can access. If you prefer to use these instead of external pull-up resistors, you can use the
analogWrite
- Writes an analog value (PWM wave) to a pin.
random
- The random function generates pseudo-random numbers.
digitalRead
- Reads the value from the specified pin, either
HIGH
orLOW
- Reads the value from the specified pin, either
digitalWrite
- Writes a
HIGH
or aLOW
to a digital pin.
- Writes a
delay
- Pauses the program for the amount of time (in milliseconds) specified as parameter.
Code
int ledPin = 5; // Red LED involved with switches is connected to pin 5
int buttonApin = 9;// Button 1 connected to PIN 9
int buttonBpin = 8; // Button 2 connected to PIN 8
byte leds = 0;
int buzzer = 12;//the pin of the active buzzer
void setup() {
// set the mode of the pins
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
pinMode(A4,OUTPUT);
pinMode(A5,OUTPUT);
pinMode(A6,OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(A1,random(255));
analogWrite(A2,random(255));
analogWrite(A3,random(255));
analogWrite(A4,random(255));
analogWrite(A5,random(255));
analogWrite(A6,random(255));
if (digitalRead(buttonApin) == LOW) {
digitalWrite(ledPin, HIGH);
}
if (digitalRead(buttonBpin) == LOW) {
digitalWrite(ledPin, LOW);
}
unsigned char i;
//output an frequency
for(i=0;i<200;i++) {
digitalWrite(buzzer,HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
}
//output another frequency
for(i=0;i<200;i++) {
digitalWrite(buzzer,HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer,LOW);
delay(2);//wait for 2ms
}
delay(100);
}
Comments
You can read more about how comments are sorted in this blog post.
User Comments
There are currently no comments for this article.