Hello everyone!
I'm working on a project which includes an arduino nano, an mcp3201 adc, and a 44780 lcd. A strange problem is occurring when reading values from the adc - i can read the values through the serial monitor, but then symbols start printing on the lcd, even when lcd.print() is deleted from the code. I also noticed that trying to display anything on the lcd after spi communication is established is always not happening, as if it is interfering with it.
#include <hd44780.h>
#include <hd44780ioClass/hd44780_pinIO.h>
#include <SPI.h>
const int rs=A4, en=11, db4=A3, db5=9, db6=8, db7=7; // for all other devices
hd44780_pinIO lcd(rs, en, db4, db5, db6, db7);
const byte DAT = 12; // SPI MISO Pin
const byte CLK = 13; // SPI Clock Pin
const byte CS = 10; // SPI SS Pin (Chip Select)
void setup()
{
Serial.begin(9600);
lcd.begin(40, 2);
lcd.setCursor(0, 0);
lcd.print("Voltage:");
SPI.beginTransaction(SPISettings(1500000, MSBFIRST, SPI_MODE0));
SPI.begin();
pinMode(DAT,INPUT);
pinMode(CS,OUTPUT);
digitalWrite(CS,LOW); // CS pin is cycled low then high because
digitalWrite(CS,HIGH); // the PWR ON state of the pin is unknown.
digitalWrite(CLK,LOW);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
}
void loop()
{
unsigned int reading = 0;
digitalWrite(CS,LOW);
reading = SPI.transfer16(0x0000); // "reading" captured, but it needs
digitalWrite(CS,HIGH); // the following 2 line to clean it up:
//*********** IMPORTANT PART: *************************************
reading = reading << 3; //Must shift out the 3 MSB's (trash bits)
reading = reading >> 4; //Must shift back 4 MSB's (pushes off LSB)
//The above two lines clears the top 3 MSB that are unknown "trash",
// then shifts back 4 bits to not only bring in all "0" for the 4
// MSB's, but ALSO pushes off the LSB which came in from the LBS
// reading that is transmitted in LSB order (another "trash" bit).
//*****************************************************************
// If using another reference voltage, replace the following
// "4.096" with the actual voltage used for reference.
float voltage = reading * (4.096 / 4095); // Using 4.096V Ref IC
Serial.println(voltage, 3); // voltage shown on terminal (1mv res.)
lcd.setCursor(0, 1);
lcd.print(reading,3);
lcd.print(" V ");
delay(250); // Adjust delay as needed
}

