Sounding the Shofar in Elul (Torah in a Minute)

(א) דיני ימי תחנונים וערב ראש השנה ובו ד"ס:
נוהגים לקום באשמור' לומר סליחו' ותחנונים מראש חדש אלול ואילך עד יום הכפורים: הגה ומנהג בני אשכנז אינו כן אלא מראש חודש ואילך מתחילין לתקוע אחר התפלה שחרית ויש מקומות שתוקעין ג"כ ערבית ועומדים באשמורת לומר סליחות ביום א' שלפני ר"ה ואם חל ר"ה ב' וג' אז מתחילין מיום א שבוע שלפניו (מנהגים) ...:

(1) We have the custom to get up at the break of dawn to recite Selichot and tachanunim from Rosh Chodesh Elul until Yom Kippur. (Rema: But the custom of the Ashkenazi Jews is not so. Rather, from Rosh Chodesh on, we blow the Shofar after Shacharit, and there are some places where they also blow the Shofar after Arvit. We get up at dawn to recite Selichot from the Sunday before Rosh HaShanah on; and if Rosh HaShanah falls on a Monday or Tuesday, we begin reciting Selichot from Sunday the week before Rosh HaShanah....

ר' יהושע בן קרחה אומר ארבעים יום עשה משה בהר קורא בדת מקרא ביום ושונה בדת מקרא בלילה, ולאחר ארבעים יום לקח את הלוחות וירד אל המחנה ובשבעה עשר בתמוז שבר את הלוחות והרג את לייטי ישראל ועשה ארבעים יום במחנה עד ששרף את העגל וכתתו כעפר הארץ והרג את כל אשר נשק לעגל והכרית ע"ז מישראל והתקין כל שבט במקומו, ובראש חדש אלול אמר לו הקב"ה (שמות כד, יב): "עֲלֵה אֵלַי הָהָרָה" והעבירו שופר במחנה, שהרי משה עלה להר שלא יטעו עוד אחר עכומ"ז והקב"ה נתעלה אותו היום באותו שופר, שנאמר (תהלים מז, ו): "עָלָה אֱלֹקִים בִּתְרוּעָה", ועל כן התקינו חכמים שיהיו תוקעים בשופר בראש חדש אלול בכל שנה ושנה.

Rabbi Joshua, son of Ḳorchah, said: Forty days was Moses on the mountain, reading the Written Law by day, and studying the Oral Law by night. After the forty days he took the tables (of the Law) and descended into the camp on the 17th of Tammuz, and he broke in pieces the tables, and slew the sinners in Israel. He then spent forty days in the camp, until he had burnt the calf, and powdered it like the dust of the earth, and he had destroyed the idol worship from Israel, and he instituted every tribe in its place. And on the New Moon of Elul the Holy One, blessed be He, said to him: "Come up || to me on the mount" (Ex. xxiv. 12), and let them sound the Shofar (ram's horn) throughout the camp, for, behold, Moses has ascended the mount, so that they do not go astray again after the worship of idols. The Holy One, blessed be He, was exalted with that Shofar, as it is said, "God is exalted with a blast, the Lord with the sound of the shofar" (Ps. xlvii. 5). Therefore the sages instituted that the Shofar should be sounded on the New Moon of Ellul every year.

הלכות ראש השנה
תניא בפרקי ר"א בר"ח אלול אמר הקב"ה למשה עלה אלי ההרה שאז עלה לקבל לוחות אחרונות והעבירו שופר במחנה משה עלה להר שלא יטעו עוד אחר ע"ג והקב"ה נתעלה באותו שופר שנאמר (תהלים מ"ז) עלה אלקים בתרועה וגו' לכן התקינו חז"ל שיהו תוקעין בר"ח אלול בכל שנה ושנה וכל החדש כדי להזהיר ישראל שיעשו תשובה שנאמר (עמוס ג') אם יתקע שופר בעיר וגו' ...

Halachot of Rosh Hashanah

(The Tur quotes Pirkei deRabbi Eliezer, see above)

...For this reason, the Sages of blessed memory instituted that the shofar be blown on Rosh Chodesh Elul each and every year and the entire month in order to alert Israel to perform Teshuvah/repentance, as it is said, "When a ram's horn is sounded in a town, [do the people not take alarm?]..." (Amos 3:6).

אִם־יִתָּקַע שׁוֹפָר בְּעִיר וְעָם לֹא יֶחֱרָדוּ אִם־תִּהְיֶה רָעָה בְּעִיר וַיי לֹא עָשָׂה׃

When a ram’s horn is sounded in a town, Do the people not take alarm? Can misfortune come to a town If the LORD has not caused it?