How to use computers to build devices!
2025-03-25
OFF if no current delivered to the Base
ON when even a tiny bit of current flows to the Base
Components:
// Tell the computer where the LED is
#define LED_PIN 4 // The LED is on pin "5"
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_PIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_PIN, HIGH); // turn the LED on (voltage HIGH)
delay(1000); // wait for 1 second
digitalWrite(LED_PIN, LOW); // turn the LED off (voltage LOW)
delay(1000); // wait for 1 second
}
// Tell the computer where the button and the LED are.
#define BUTTON_PIN 0 // The button is on pin "0"
#define LED_PIN 4 // The LED is on pin "5"
// Setup runs when the computer turns on
void setup() {
// Tell the computer what to do with the button and LED
pinMode(BUTTON_PIN, INPUT); // Button with pull-up
pinMode(LED_PIN, OUTPUT); // LED for feedback
digitalWrite(LED_PIN, LOW); // Start with LED off
}
// the loop function runs over and over again forever
void loop() {
// If the Button is pressed, turn LED on
if (digitalRead(BUTTON_PIN) == HIGH) {
digitalWrite(LED_PIN, HIGH); // Turn LED on
}
// If the Button is NOT pressed, turn LED off
if (digitalRead(BUTTON_PIN) == LOW) {
digitalWrite(LED_PIN, LOW); // Turn LED off
}
}
#include <Arduino.h>
// Tell the computer where the potentiometer and LED are
#define Potentiometer_Pin 3 // The potentiometer is on pin "3"
#define LED_PIN 4 // The LED is on pin "5"
// Some information to help the computer make the LED's brightness adjustable
const int LEDC_CHANNEL = 0; // Use PWM channel 0
const int LEDC_TIMER_BIT = 8; // 8-bit resolution (0-255)
const int LEDC_BASE_FREQ = 5000; // 5 kHz frequency
// Run the startup instructions for the computer
void setup() {
// Tell the computer what to do with the LED
ledcSetup(LEDC_CHANNEL, LEDC_BASE_FREQ, LEDC_TIMER_BIT);
ledcAttachPin(LED_PIN, LEDC_CHANNEL);
// Apply a brightness of "0" to the LED to start
ledcWrite(LEDC_CHANNEL, 0); // Apply PWM value to LED strand
}
// Instructions to repeat over and over
void loop() {
// read the value from the potentiometer:
int potValue = analogRead(Potentiometer_Pin); // Read the potentiometer value, save it
int LEDbrightness = map(abs(potValue), 0, 4095, 0, 255); // Convert the potentiometer value to a brightness
// Turn on the LED at the calculated brightness level
ledcWrite(LEDC_CHANNEL, LEDbrightness);
}