Arduino LED+Servo Circuit

Task:

create a program for Arduino, using 2 or more familiar details (LEDs, button, Servo, photoresistor)

Hardware:

Software:

Arduino


The idea was to create a circuit, using Servo and LED, in a way that potentiometer would control the rotation of the servo as well as the brightness of LED.

I started with the tests of circuits with servo and LED separately.

#define LED_PIN 9
#define POT_PIN A0

void setup() {
pinMode (LED_PIN, OUTPUT);
pinMode (POT_PIN, INPUT);
}

void loop() {
  int rotation, brightness;
  rotation = analogRead (POT_PIN);
  brightness = rotation / 4;
  analogWrite (LED_PIN, brightness);
}
#include <Servo.h>
#define POT_MAX_ANGLE 270.0
Servo myServo;

void setup() {
myServo.attach (9);
}

void loop() {
  int val= analogRead (A0);
  int angle = int(val / 1024.0 * POT_MAX_ANGLE);
  angle = constrain (angle, 0, 180);
  myServo.write(angle);
}

Onwards, I have tried to get them together in one unit.

I combined each of the blocks (‘define’, void setup, void loop) and it worked out perfectly.

Here is how the circuit works:

And the program itself:

#include <Servo.h>
#define POT_MAX_ANGLE 270.0
Servo myServo;

#define LED_PIN 5
#define POT_PIN A0

void setup ()
{
  myServo.attach(9);
  pinMode(LED_PIN, OUTPUT);
  pinMode(POT_PIN, INPUT);
}

void loop()
{ 
  int rotation, brightness;
  rotation = analogRead(POT_PIN);
  brightness = rotation / 4;
  analogWrite(LED_PIN, brightness);
  
  int val= analogRead (A0);
  int angle = int(val / 1024.0 * POT_MAX_ANGLE);
  angle = constrain (angle, 0, 180);
  myServo.write(angle);
}

Tags: , , ,

Comments (1)

  • Kamilia Iusupova

    |

    Please, set the featured image.

    Reply

Leave a comment

address: 20 Myasnitskaya ulitsa
(metro stations ‘Lubyanka’ and ‘Kitay-Gorod’)
Moscow 101000 Russia

phone: +7(495)772-95-90 *15026

email: city@hse.ru