Hello fellow humans!
I'm currently taking the challenge of learning about I2C, and have now learnt about this for a couple of days.
It's time for my first circuit!
I'm going to learn by making several circuits doing different things, and at the same time learn what is happening.
The first thing I'm trying to do is simple to access and configure the IODIRB chip of my MCP23018, (datasheet link) which I understand sets port side B's pins (GPB0-GPB7) as either outputs or inputs, and by reading the datasheet: (text 1.6.1, page 18)
I understand that writing to the IODIRX SETS the IO pins low/high, and if I want to READ them I use GPIOX.
I'm using the Wire.h (link here) library, and I made this code, added below. I commented out what I understand (or think?) the code is doing, and I also put a lot of Serial prints for debugging with my computer.
Would love if someone could try to help me get this working, because it isn't..
Here is the Serial outputs I get:
..so I guess the code isn't THAT BAD as it seems to be a possible code for something o.o
Thank you all in advance! U guys are the best :3
I'm currently taking the challenge of learning about I2C, and have now learnt about this for a couple of days.
It's time for my first circuit!
I'm going to learn by making several circuits doing different things, and at the same time learn what is happening.
The first thing I'm trying to do is simple to access and configure the IODIRB chip of my MCP23018, (datasheet link) which I understand sets port side B's pins (GPB0-GPB7) as either outputs or inputs, and by reading the datasheet: (text 1.6.1, page 18)
.. I found that this is the way to do this. I'm trying to set all the GPBX as outputs, and I connected GPB0 to my Arduino UNO's analog input pin A0, so I can read it and see if the voltage is HIGH.1.6.1 I/O DIRECTION REGISTER
Controls the direction of the data I/O.
When a bit is set, the corresponding pin becomes an input. When a bit is clear, the corresponding pin becomes an output
I understand that writing to the IODIRX SETS the IO pins low/high, and if I want to READ them I use GPIOX.
I'm using the Wire.h (link here) library, and I made this code, added below. I commented out what I understand (or think?) the code is doing, and I also put a lot of Serial prints for debugging with my computer.
Code:
#include <Wire.h>
void setup() {
Serial.begin(9600);
while(!Serial); //wait for Serial to start
Serial.print("Voltage at Arduino analog A0 BEFORE: ");
Serial.println(analogRead(A0));
Serial.println("------------------------");
Serial.println("WIRE: beginning");
Wire.begin(); //join i2c bus (no parameters=join as master)
Serial.println("WIRE: started");
Wire.beginTransmission(0x20); //begin transmission with MCP23018. 0x20 is the adress for the MCP23018 in hexadecimal, with ADDR connected to ground.
Serial.println("WIRE: transmission begun");
Wire.write(0x00); //register address to talk to
//0x01=IODIRB register address (with help from the datasheet, page.7 TABLE 1-1)
Serial.println("WIRE: wrote register address");
Wire.write(0x00); //00000000=0x00 in hexadecimal
//=sets GPIO-B0 to PPIO-B7 as outputs?
Serial.println("WIRE: GPIO-B5 set as output?");
Wire.endTransmission();
Serial.println("WIRE: transmission ended");
Serial.println("------------------------");
Serial.print("Voltage at Arduino analog A0 AFTER: ");
Serial.println(analogRead(A0));
Serial.println("------------------------");
}
void loop() {
delay(1000); //we no need this C:
}
Here is the Serial outputs I get:
Code:
Voltage at Arduino analog A0 BEFORE: 1
------------------------
WIRE: beginning
WIRE: started
WIRE: transmission begun
WIRE: wrote register address
WIRE: GPIO-B5 set as output?
WIRE: transmission ended
------------------------
Voltage at Arduino analog A0 AFTER: 0
------------------------
Thank you all in advance! U guys are the best :3