Group Members: Maliha Nahiyan Srotosshini & Shamsa Alremeithi
Exercise 1
task: make something that uses only one sensor on arduino and makes an ellipse (or other shape) in p5 move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by p5
For this exercise, we had connected a potentiometer to the Arduino by connecting the middle pin to analog pin A1, and the other two pins to 5V and GND. we had written a simple Arduino code to read the analog value from the potentiometer and map it to a range of 0 to 400, which was then sent to the computer through the serial port. With p5.js and the p5.webserial library, a circle moves left to right across the screen based on the potentiometer’s position. we also included “Connect” and “Disconnect” buttons to control the serial connection from the browser with ease.
arduino code
void setup() { Serial.begin(9600); // Initialize serial communication at 9600 baud rate } void loop() { // Read the analog value from pin A1 (range: 0 to 1023) int potentiometer = analogRead(A1); // Map the potentiometer value (0-1023) to a new range (0-400) int mappedPotValue = map(potentiometer, 0, 1023, 0, 400); // Send the mapped value to p5.js via serial Serial.println(mappedPotValue); delay(100); // Wait for 100 milliseconds before the next reading } ); }
P5.js code
let port; let connectBtn; let disconnectBtn; let baudrate = 9600; let isConnected = false; function setup() { createCanvas(400, 400); background(220); // Create a new Web Serial port instance using p5.webserial port = createSerial(); // If a port was previously used, auto-connect to it let usedPorts = usedSerialPorts(); if (usedPorts.length > 0) { port.open(usedPorts[0], baudrate); isConnected = true; } // Create the Connect button and open the port when clicked connectBtn = createButton("Connect to Serial"); connectBtn.position(10, 10); connectBtn.mousePressed(() => { port.open(baudrate); // Opens a serial connection using the chosen baud rate isConnected = true; }); // Create the Disconnect button to close the serial port disconnectBtn = createButton("Disconnect"); disconnectBtn.position(150, 10); disconnectBtn.mousePressed(() => { port.close(); // Closes the serial connection isConnected = false; // Clear screen and show "Disconnected" message background(255); textAlign(CENTER, CENTER); textSize(18); fill(100); text("Disconnected.", width / 2, height / 2); }); } function draw() { if (isConnected) { // Read until newline character let str = port.readUntil("\n"); if (str.length > 0) { background("white"); // Convert the received string to an integer (e.g., mapped potentiometer value) let x = int(str); // Make sure x stays within the canvas width (safety measure) x = constrain(x, 0, width); // Draw an ellipse at the position based on incoming data ellipse(x, 200, 40, 40); } } }
Excercise 2
task:
make something that controls the LED brightness from p5
p5.js interface:
Arduino Code:
int ledPin = 9; // PWM-capable pin to control LED brightness void setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate pinMode(ledPin, OUTPUT); // Set the LED pin as an output } void loop() { if (Serial.available()) { // Check if data is available to read from serial int brightness = Serial.parseInt(); // Read the integer value (brightness) brightness = constrain(brightness, 0, 255); // Limit the value to the 0-255 range analogWrite(ledPin, brightness); // Write the brightness value to the LED pin } }
This project creates a real-time visual and physical interface to control an LED’s brightness using a slider in a p5.js sketch. The brightness value is sent from the browser to an Arduino board via serial communication. As the user moves the slider, the LED’s intensity changes accordingly, both in the physical circuit and on-screen through a glowing animation and gauge ring. The interface also includes a connect/disconnect button for flexible hardware control.