Saturday, September 22, 2018

Arduino : Intermediate - OLED I2C with Servo Motor

Description
Demonstrates the use of 128x64 I2C OLED and Servo Motor to display basic instruction. You can download OLED I2C library at Rinky-Dink Electronic.

List Item
  1. Arduino Uno 
  2. 128x64 I2C OLED  
  3. Pushbutton
  4. Servo Motor
  5. Wire Jumper

Wiring Diagram

Servo Motor Pin Diagram

Video




Coding

#include <OLED_I2C.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // twelve servo objects can be created on most boards

OLED  myOLED(SDA, SCL, 8);

int pos=0;    // variable to store the servo position
extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];


void setup()
{
  myservo.attach(9);   // attaches the servo on pin 9 to the servo object
  myOLED.begin();      //Initialize the display and screen buffer
}

void loop()
{
  for (pos = 0; pos <= 180; pos += 1)    // goes from 0 degrees to 180 degrees
  {
    // in steps of 1 degree
    myservo.write(pos);                  // tell servo to go to position in variable 'pos'     
    delay(30);                           // waits 15ms for the servo to reach the position
    myOLED.clrScr();                       // clear the screen buffer
    myOLED.setFont(SmallFont);
    myOLED.print("Servo Turn", CENTER, 0); // print a string at the specified coordinate in the screen buffer
    myOLED.setFont(MediumNumbers);
    myOLED.printNumI(pos, CENTER, 35);
    myOLED.update();                       // send to screen buffer
  }

  delay (1000);

  for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
  {
    myservo.write(pos);                  // tell servo to go to position in variable 'pos'
    delay(15);                           // waits 15ms for the servo to reach the position
    myOLED.clrScr();                       // clear the screen buffer
    myOLED.setFont(SmallFont);
    myOLED.print("Servo Turn", CENTER, 0); // print a string at the specified coordinate in the screen buffer
    myOLED.setFont(MediumNumbers);
    myOLED.printNumI(pos, CENTER, 35);
    myOLED.update();                       // send to screen buffer
  }

  delay (1000);

}

No comments:

Post a Comment