This morning I jumped out of bed at 7am (like at 9 year old boy), brewed a coffee (like a middle-aged man) and set about playing with my new Arduino (like a 9 year old boy). After flying through a few basic tutorials, I was ready to go it alone and try building something off my own back.
This isn't strictly an Internet of Things project but just 2 hours into learning about electronics I have made something work, and I'm proud of myself so thought I'd knock out a quick blog post.
What does it do?
I decided I wanted to make a series of LED's represent the resistance across a potentiometer - not exactly groundbreaking but it was a challenge I felt I could achieve before my pot of coffee went cold.
Kit I used on this project:
- 1 x Arduino UNO r3
- 1 x Breadboard
- 1 x Potentiometer
- 3 x coloured LED's
- 3 x 220 Ohm resistors
- A load of hook-up wires
- USB cable (for power and data)
- Laptop
- Offical Arduino IDE
- Limited imagination
- Programing knowledge
Project video
How does it work
I wrote a simple script that does the following:
- Initiates serial comms on startup
- Runs a continuous loop that reads analog input from the Potentiometer sensor
- Checks the sensor value on each pass of the loop
- Turns on the correct LED based on sensor value
- If high value (>1000) run another loop to make the red LED flash
I also created a simple method called clearLEDs()
that is called whenever I want all LED's to be switched off.
In pictures
The code
This is a thrown-together script to get something working but hey it works so serves its purpose.
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int fadeValue = 250;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
if (sensorValue < 5){
clearLEDs();
}
else if (sensorValue < 500){
clearLEDs();
analogWrite(bluePin, fadeValue);
}
else if (sensorValue < 900){
clearLEDs();
analogWrite(greenPin, fadeValue);
}
else{
clearLEDs();
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(redPin, fadeValue);
delay(2);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(redPin, fadeValue);
delay(5);
}
}
delay(100);
}
void clearLEDs() {
for (int thisPin = 9; thisPin < 12; thisPin++) {
analogWrite(thisPin, 0);
}
}
This is a very simple "Hello World" project but I'm happy with my first ever Arduino project. The best part is I managed to not blow anything up!
Next up is my first real IoT project!