Analogic and digitals pin

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.

Pure sine wave is not possible, but maybe some kind of software pwm wave that looks similar to sine wave...

The source code by my professor is the following one:

const uint8_t pin = 3;

double f = 1.0; // signal frequency

double t = 0.0; // time variable (updated in the loop)

void setup()
{
    analogWriteResolution(12);
    pinMode(pin, OUTPUT);
    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, 4095);
    analogWrite(pin, 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;
}

As you can see he is trying to pull out the sine wave from A0

  • A0 is not a PWM pin. You should Google PWM.

Tell him that the code does not meet his specification as it uses the wrong pin

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?

  • No

I was wrong to upload source code, sorry guys

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;
}

As you can see the problem is the same

Hi,
If you read the posts, the professor and @angelsblack don't specify what model/make of controller.

analogWrite as PWM is unique to the Arduino world as in a UNO/Nano, in a logical world it means analog level output.

The professor I would sat is talking generally, where analogWrite does just that and not Arduino's little foible of PWM.

We do not know the exact question @angelsblack actually asked chatGPT.

Tom.. :smiley: :+1: :coffee: :australia:

I'm using Arduino UNO from tinkercad
My question that I asked to chatGPT is: can I use analogWrite on an analogic pin like A0?

I'm using Arduino UNO from tinkercad. If the pin A0 has a DAC can I use analogWrite on it?

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.

An Arduino Uno also doesn't do analogWriteResolution(xxxx) so the code here wouldn't compile on a Tinkercad Uno:

Is bit-banging PWM an option?

  • This just might be a matter of a professor who has exceeded their level of competency.

post 32. how do you want me to attrib you for the code

The Sinusoidal PWM program in #11 looks like you can filter an Uno's digital PWM output through an RC filter and get an analog sine wave: