New Year’s project

I received some servo motors for Christmas, so naturally I need to start working on a robot. Details to come on the project, but for now I’ve been trying to figure out how to (a) control two servos with an ATTiny85 microcontroller and (b) talk to that microcontroller with an Arduino that is telling the ATTiny85 how to position the servos.

It took the better part of several days (including some learning and additional trips down rabbit holes, but here we go.

Arduino communicating (via I2C) with an ATTiny85 that is controlling two servo motors (empty shot glass for scale)

If you care, here’s the code for the ATTiny85 and Arduino. The ATTiny Libraries are from here for TinyWireS and here for Servo8Bit. For the latter, check issue #6 on how to make it compile properly on the latest versions of the IDE>

#include "Servo8Bit.h"
#include "TinyWireS.h" 

#define I2C_SLAVE_ADDR  0x26 

Servo8Bit base;
Servo8Bit head;

void setup() {
  base.attach(4);
  head.attach(3);
  TinyWireS.begin(I2C_SLAVE_ADDR);
  base.write(90);
  head.write(90);
  delay(15);
}

void loop(){

  if (TinyWireS.available()) {
    byte degree = TinyWireS.receive();
    base.write(degree);
    head.write(degree);
  }
  
}
#include <Wire.h>
#define I2C_SLAVE_ADDR  0x26  

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(115200);
  Serial.println("Ready");
}

void loop() {
  for(int i = 70; i < 110; i++){
    Wire.beginTransmission(I2C_SLAVE_ADDR);
    Wire.write((byte)i);
    Wire.endTransmission();
    delay(100);
  }
  for(int i=110; i >= 70; i--){
    Wire.beginTransmission(I2C_SLAVE_ADDR);
    Wire.write((byte)i);
    Wire.endTransmission();
    delay(100);
  }

}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.