I2Cが上手く動作しない場合はファームウェアをアップデートしてください。 こちらのhexファイル

温湿度センサ(MCP9808)

温度センサ:https://www.switch-science.com/catalog/3986/

while true
	I2C.write(0x18, 0x08, 0x03)
	ans = I2C.read(0x18,0x05,2)
	a = ans[1] | ((ans[0] & 0x1f)<<8)
	a = (a.to_f)* 0.0625
	puts("ans:"+a.to_s+"\r\n")
	sleep(5)
end
			

モータードライバ(DRV8830)

https://www.switch-science.com/catalog/2510/

class Moter
  def init()
    I2C.write(0x62,0x01,0x80)
    I2C.write(0x60,0x01,0x80)
  end
  
  def r(speed,r)
    I2C.write(0x62,0x00,speed+r)
  end
  
  def l(speed,l)
    I2C.write(0x60,0x00,speed+l)
  end

  def control(speed,r,l)
    I2C.write(0x62,0x00,speed+r)
    I2C.write(0x60,0x00,speed+l)
  end
end
			

インスタンスがどうこう


class Moter
def initialize(n)
	@num = n
	I2C.write(0x60+@num,0x01,0x80)
end

def control(speed,status)
	I2C.write(0x60+@num,0x00,speed+status)
end
end
			

サーボモータードライバ(PCA9685)

通常品:https://www.amazon.co.jp/dp/B01D1D0CX2
GROVE:https://item.rakuten.co.jp/robotshopjapan/rb-see-679/

$moters = I2C.new()
def module_init()
	$moters.write(0x40, 0xFA, 0x00)
	$moters.write(0x40, 0xFB, 0x00)
	$moters.write(0x40, 0xFC, 0x00)
	$moters.write(0x40, 0xFD, 0x00)
	$moters.write(0x40, 0x01, 0x04)
	$moters.write(0x40, 0x00, 0x01)
	sleep_ms(50)
end

def freq_set()
	$moters.write(0x40, 0x00, 0x11)
	$moters.write(0x40, 0xFE, 0x65)
	$moters.write(0x40, 0x00, 0x01)
	sleep_ms(50)
	$moters.write(0x40, 0x00, 0x81)
end

def m_all_move(angle)
	angle = (angle/180.0*500 + 150).to_i
	$moters.write(0x40, 0xFA, 0x00)
	$moters.write(0x40, 0xFB, 0x00)
	$moters.write(0x40, 0xFC, angle & 0xff)
	$moters.write(0x40, 0xFD, angle >>8)
end

def m_move(ch,angle)
	angle = (angle/180.0*500 + 150).to_i
	$moters.write(0x40, ch*4+6, 0x00)
	$moters.write(0x40, ch*4+7, 0x00)
	$moters.write(0x40, ch*4+8, angle & 0xff)
	$moters.write(0x40, ch*4+9, angle >>8)
end

module_init()
freq_set()
m_all_move(90)
while true
	m_move(6,45)
	m_move(8,135)
	m_move(10,90)
	m_move(11,90)
	sleep(2)
	m_move(6,135)
	m_move(8,45)
	m_move(10,135)
	m_move(11,45)
	sleep(2)
end
			

OLED

https://www.switch-science.com/catalog/829/
ASCIIコード
スペース(32)~Z(90)まで対応

class OLED
$oled = 0
def init()
	$oled = I2C.new
	$oled.write(0x3c,0x00,0xAE,0xA8,0x3F)
	$oled.write(0x3c,0x00,0xD3,0x00)
	$oled.write(0x3c,0x00,0x40,0xA0,0xC0,0xDA,0x12)
	$oled.write(0x3c,0x00,0x81,0xFF)
	$oled.write(0x3c,0x00,0xA4,0xA6,0xD5,0x80)
	$oled.write(0x3c,0x00,0x2E,0x20,0x10)
	$oled.write(0x3c,0x00,0x21,0,127)
	$oled.write(0x3c,0x00,0x22,0,7)
	$oled.write(0x3c,0x00,0x8D,0x14,0xAF)
	i = 0
	while i<8
		$oled.write(0x3C,0x80,0xB0 + i)
		j = 0
		while j<16
			$oled.write(0x3C,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00)
			j = j+1
		end
		i = i+1
	end
end
def send(i,name)
	a=[
	[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00],
	[0x00, 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00],
	[0x00, 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00, 0x00],
	[0x00, 0x23, 0x13, 0x08, 0x64, 0x62, 0x00, 0x00],
	[0x00, 0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00],
	[0x00, 0x00, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x1C, 0x22, 0x41, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x41, 0x22, 0x1C, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x08, 0x2A, 0x1C, 0x2A, 0x08, 0x00, 0x00],
	[0x00, 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, 0x00],
	[0x00, 0xA0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00],
	[0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, 0x00],
	[0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00, 0x00],
	[0x00, 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00, 0x00],
	[0x00, 0x62, 0x51, 0x49, 0x49, 0x46, 0x00, 0x00],
	[0x00, 0x22, 0x41, 0x49, 0x49, 0x36, 0x00, 0x00],
	[0x00, 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00, 0x00],
	[0x00, 0x27, 0x45, 0x45, 0x45, 0x39, 0x00, 0x00],
	[0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00, 0x00],
	[0x00, 0x01, 0x71, 0x09, 0x05, 0x03, 0x00, 0x00],
	[0x00, 0x36, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00],
	[0x00, 0x06, 0x49, 0x49, 0x29, 0x1E, 0x00, 0x00],
	[0x00, 0x00, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x00, 0xAC, 0x6C, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00, 0x00],
	[0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00, 0x00],
	[0x00, 0x41, 0x22, 0x14, 0x08, 0x00, 0x00, 0x00],
	[0x00, 0x02, 0x01, 0x51, 0x09, 0x06, 0x00, 0x00],
	[0x00, 0x32, 0x49, 0x79, 0x41, 0x3E, 0x00, 0x00],
	[0x00, 0x7E, 0x09, 0x09, 0x09, 0x7E, 0x00, 0x00],
	[0x00, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00],
	[0x00, 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00, 0x00],
	[0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C, 0x00, 0x00],
	[0x00, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00, 0x00],
	[0x00, 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00, 0x00],
	[0x00, 0x3E, 0x41, 0x41, 0x51, 0x72, 0x00, 0x00],
	[0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00, 0x00],
	[0x00, 0x41, 0x7F, 0x41, 0x00, 0x00, 0x00, 0x00],
	[0x00, 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00, 0x00],
	[0x00, 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00],
	[0x00, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00],
	[0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F, 0x00, 0x00],
	[0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00, 0x00],
	[0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00, 0x00],
	[0x00, 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00, 0x00],
	[0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00, 0x00],
	[0x00, 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00, 0x00],
	[0x00, 0x26, 0x49, 0x49, 0x49, 0x32, 0x00, 0x00],
	[0x00, 0x01, 0x01, 0x7F, 0x01, 0x01, 0x00, 0x00],
	[0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00, 0x00],
	[0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00, 0x00],
	[0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00, 0x00],
	[0x00, 0x63, 0x14, 0x08, 0x14, 0x63, 0x00, 0x00],
	[0x00, 0x03, 0x04, 0x78, 0x04, 0x03, 0x00, 0x00],
	[0x00, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00, 0x00],
]

	$oled.write(0x3C,0x00,0xB0+i,0x21,0x00,0x7F)
	j = 0
	while(j<16)
		b = (name[j].ord) - 32
		$oled.write(0x3C,0x40,a[b][0],a[b][1],a[b][2],a[b][3],a[b][4],a[b][5],a[b][6],a[b][7])
		j = j+1
	end
end
end
oled = OLED.new
# 初期化
oled.init()
# oled.send(行,表示文字列16文字)
oled.send(0,"LETS!GO!MRUBY/C!")
oled.send(1,"================")
oled.send(2,"ALL HAPPY       ")
			

2値画像表示


class OLED
	$oled = 0
	def init()
		$oled = I2C.new
		$oled.write(0x3c,0x00,0xAE,0xA8,0x3F)
		$oled.write(0x3c,0x00,0xD3,0x00)
		$oled.write(0x3c,0x00,0x40,0xA0,0xC0,0xDA,0x12)
		$oled.write(0x3c,0x00,0x81,0xFF)
		$oled.write(0x3c,0x00,0xA4,0xA6,0xD5,0x80)
		$oled.write(0x3c,0x00,0x2E,0x20,0x10)
		$oled.write(0x3c,0x00,0x21,0,127)
		$oled.write(0x3c,0x00,0x22,0,7)
		$oled.write(0x3c,0x00,0x8D,0x14,0xAF)
		i = 0
		while i<9
			$oled.write(0x3C,0x80,0xB0 + i)
			j = 0
			while j<16
				$oled.write(0x3C,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00)
				j = j+1
			end
			i = i+1
		end
	end
	def send(a)
		$oled.write(0x3C,0x00,0xB0+i,0x21,0x00,0x7F)
		c = 0
		while(c<7)
			j = 0
			while(j<16)
				b = j
				$oled.write(0x3C,0x40,a[c][b][0],a[c][b][1],a[c][b][2],a[c][b][3],a[c][b][4],a[c][b][5],a[c][b][6],a[c][b][7])
				j = j+1
			end
			c =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FF,0xFF,0xFE,0xF8],[0xE0,0x83,0x0F,0x3F,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x1F],[0x87,0xE1,0xF8,0xFE,0xFF,0xFF,0xFF,0x7F],[0x3F,0x1F,0x0F,0x07,0x03,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00, 0x7C, 0x04, 0x18, 0x04, 0x78, 0x00, 0x00],[0x00, 0x00, 0x7C, 0x08, 0x04, 0x00, 0x00, 0x00],[0x00, 0x3C, 0x40, 0x40, 0x7C, 0x00, 0x00, 0x00],[0x00, 0x7F, 0x48, 0x44, 0x44, 0x38, 0x00, 0x00],[0x00, 0x1C, 0xA0, 0xA0, 0x7C, 0x00, 0x00, 0x00],[0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, 0x00],[0x00, 0x38, 0x44, 0x44, 0x28, 0x00, 0x00, 0x00],],
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x01,0x03,0x0F,0x1F],[0x3F,0x7F,0xFE,0xF8,0xE0,0x83,0x0F,0x3F],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0x7F,0x1F,0x07,0xC1,0xF0,0xFE],[0xFF,0x7F,0x1F,0x0F,0x07,0x03,0x01,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]],
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x03,0x07,0x0F,0x1E,0x38],[0x60,0x83,0x0F,0x3F,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x1F],[0x07,0xC1,0x70,0x3C,0x1F,0x07,0x03,0x01],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]],
[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x01,0x02,0x00,0x00,0x07,0x1F,0xFF],
[0xFF,0xFF,0xFF,0x1F,0x07,0x01,0x00,0x00],
[0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]]
]
oled = OLED.new
# 初期化
oled.init()
# oled.send(画像配列8×16×8)
oled.send(picture)
			

LCD

LCD:https://www.switch-science.com/catalog/1629/

$lcd = I2C.new
sleep(1)
def rgb(r,g,b)
	$lcd.write(0x62,0x00,0x00)
	$lcd.write(0x62,0x08,0xFF)
	$lcd.write(0x62,0x01,0x20)
	$lcd.write(0x62,0x04,r)
	$lcd.write(0x62,0x03,g)
	$lcd.write(0x62,0x02,b)
end
def send_txt(txt)
	$lcd.write(0x3E,0x80,0x02)
	sleep_ms(5)
	$lcd.write(0x3E,0x80,0x0C)
	sleep_ms(5)
	cnt = txt.size
	i = 0
	while(i < cnt)
	if(i == 15)
		$lcd.write(0x3E,0x80,0xC0)
	end
	$lcd.write(0x3E,0x40,(txt[i]).ord)
	i = i+1
	end
end
rgb(255,255,255)
send_txt("YoshihiroOgura")
			

Qwiic

Qwiicとは
SparkFunから販売されているI2Cにて制御するモジュール。
https://www.sparkfun.com/qwiic

赤色LEDボタン

ボタンを押したときにLEDを点灯する。

led = I2C.new
while true
	l = led.read(0x6F,0x03,1)
	s = (l[0]&0x04)>>2
	led.write(0x6F,0x19,s * 0xFF)
end
			

ToF測距センサ

VL53L0Xを搭載したレーザーの距離センサー
https://www.switch-science.com/catalog/5219/

while true
  status = 0
  I2C.write(0x29,0x00,0x01)
  while((status & 0x01) == 1)
    data = I2C.read(0x29,0x14,1)
    status = data[0]
  end
  data = I2C.read(0x29,0x14,12)
  puts(data[11] + (data[10]<<8))
  sleep_ms(100)
end