Hi guys, I am writing to you as I am having a lot of difficulty due to some doubts raised by chatGPT, precisely I have an exam the day after tomorrow and I would like to understand what is the substantial difference between digital and analog pins and between the functions (I report what I know about these functions, please correct me if I am wrong):
digitalWrite(): Allows HIGH and LOW values to be sent to the OUTPUT.
digitalRead(): Allows you to read the signal in INPUT to the pin in question (HIGH and LOW only).
analogWrite(): Allows values ranging from 0 to 255 to be sent to the OUTPUT.
analogRead(): Allows you to read the signal in INPUT to the pin (with values ranging from 0 to 1023).
I know that digital PINs are PINs that can receive or send values ranging from 0 to 255(if in PWM) or HIGH and LOW, while analog PINs can read analog data ranging from 0 to 1023. But what is the difference between the functions? I used to use the two analog functions for analog PINs and the two digital functions for digital pins (as some sites report: Notions on Input/Output - Arduino | Giuseppe Caccavale also others of course).
But using ChatGPT told me that it is not correct to use analogWrite() on the analog pins since these are used only for sensor input and that's it.
The professor explicitly asked me to generate a sine wave from an analog pin A0, so he wants to output this signal, but so is it possible to do this? I am not understanding anything, please help me.
analogRead() is reading a voltage. On an Arduino Uno the default ADC reference voltage is 5V. It's a 10-bit ADC which can read digitally 0-1023. The reading is proportional to the voltage. i.e. 5V will read 1023 and 2.5V sill read about 511 or 512.
analogWrite() is NOT true analog. It's PWM. Again assuming a 5V Arduino, If you write 0, the output will be a constant 0V (approximately). If you write 255 it will be a constant 5V (approximately).
If you write 127 or 128 it will be 0V about half the time and 5V half the time. It you have an LED (with the usual resistor) It will appear to be "half bright" because it switches faster than they eye can see. (Except your eyes & brain aren't linear so it will look brighter than that, but that's the idea.)
Most Arduinos don't have true analog so they can't put-out a sine wave without additional hardware. They also can't put-out negative voltages.
A0 should be useable as a regular digital input or output but on the Uno I'm pretty sure it doesn't support PWM.
It sounds like a trick question to emphasise the difference between an analogue pin and one that can output at least a PWM signal if not an actual analogue signal despite the name of the pin
The problem is that I have an exam with him and so I need to know how I can work as he wants hahaha
My question is: can I use analogWrite on an analog pin without PWM?
const uint8_t DACpin = A0;
double f = 100.0; // signal frequency
double t = 0.0; // time variable (updated in the loop)
void setup()
{
analogWriteResolution(10); // full DAC resolution
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
f = (double)Serial.parseFloat(); // to change frequency at runtime
}
t = micros() * 1E-6;
double wt = 2.0 * PI * f * t;
int y = mapd(sin(wt), -1.0, 1.0, 0, 1023);
analogWrite(DACpin, y);
}
/* map function working with double instead of long */
double mapd(double x, double in_min, double in_max, double out_min, double out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Thank you so much, I didn't know this difference between analogic pins.
Moreover I want to tell you all a great thank you for answers and sorry for my unspecific question.